我有下面的python代码,它是为运行名为cctfiles
的fortran代码而设计的。然后我用abedin
对正在运行的进程进行快照,并将快照写入名为sample.txt
的文件。然后我读取文件sample.txt
,直到运行的进程./cctfiles
从文件中消失。但是我注意到,即使程序cctfiles
已经退出,我的python代码也不会从while循环中中断。
下面是我的代码:
#!/usr/bin/env python
import subprocess
scb = subprocess.call
def runprog(name):
for i in xrange(1,2):
scb('cp '+name+' '+str(i), shell = 'True')
scb('sleep 1', shell = 'True')
scb('(cd '+str(i)+';'+' ./'+name+' &)', shell = 'True')
print('Job '+str(i)+' has been sent:')
# scb('cd ../', shell = 'True')
while True:
scb('sleep 5', shell = 'True')
scb('ps -ef | grep abedin > sample.txt', shell = 'True')
cnt = subprocess.Popen('grep -c ".*" sample.txt',stdout=subprocess.PIPE, shell = 'True')
(c,err) = cnt.communicate()
nlines = int(c.strip())
fl = open('sample.txt', 'r+')
count = 0
class BreakIt(Exception): pass
try:
for line in fl:
count = count + 1
for word in line.strip().split():
# print(word.strip())
if word.strip() != './'+name and count == nlines:
raise BreakIt
except BreakIt:
pass
else: break
fl.seek(0)
fl.truncate()
fl.close()
print('----------Finaly Done------------')
runprog('cctfiles')
鉴于我对Python的了解很差,任何帮助都将不胜感激!在
提前谢谢你!在
而且,这不是你问题的答案,我可能得到数百个否定点,这是你未被问到的问题的答案,»我怎么能等待许多过程同时陈述?«
因此,当您的
try/except
块接收到BreakIt
异常时,它不会做任何事情。因为BreakIt
位于while True
循环内,所以它一直循环。然后你break
对每一个不是的异常BreakIt
一个。听起来你想做相反的事。在您可能需要将
try/except
更改为:另外:我强烈建议使用内置的StopIteration,而不是你的
BreakIt
。我是说。。。您可以随意创建自己的异常,但在这种特殊情况下,您不需要该类(而且,在循环中定义类是一种不好的做法)相关问题 更多 >
编程相关推荐