目前我正在使用python os.system(cmd)来做一些日常工作。
这里有一种情况,cmd需要5-6分钟才能完成,我经常运行这个cmd,它可以工作,但当我将它放入os.system(cmd)时,os.system(cmd)会在cmd还没有完成时自动退出。
所以我的问题是:如何处理这个问题,设置超时值,或者有更好的方法来完成这项工作?
提前感谢!
发布于 2012-04-15 17:59:35
你试过subprocess模块了吗?添加它是为了在其他较旧的os方法中替换os.system。以下内容与文档中的内容非常直接:
import os
import subprocess
proc = subprocess.Popen(cmd, shell=True)
pid, sts = os.waitpid(proc.pid, 0)
# you may check on this process later and kill it if it's taking too long
if proc.poll() in [whatever, ...]:
os.kill(proc.pid)或者,如果您正在尝试调试进程退出的原因:
import subprocess
import sys
try:
retcode = subprocess.call(cmd, shell=True)
if retcode < 0:
print >>sys.stderr, "Child was terminated by signal", -retcode
else:
print >>sys.stderr, "Child returned", retcode
except OSError, e:
print >>sys.stderr, "Execution failed:", ehttps://stackoverflow.com/questions/10160961
复制相似问题