[英]Python subprocess call hangs
Python版本:2.6.7我在for循环中有以下subprocess.call,它被执行了18次,但是,这个过程一直挂在第19个循环上:
if config.get_bool_option(NAME, 'exclude_generated_code', True):
for conf in desc.iter_configs():
for gen in desc.iter_generators(conf):
generator.initialize_generated_path(gen, desc)
for genpath in gen.generated_path:
os.rename(cov_file, cov_file+'.temp')
exclude = ['lcov']
exclude += ['-r', cov_file+'.temp', '"'+genpath+'/*"']
exclude += ['-o', cov_file]
if verbose: Tracer.log.info("Running "+ ' '.join(exclude))
try:
subprocess.call(' '.join(exclude), stdout=out, stderr=out, shell=True)
except subprocess.CalledProcessError, e:
if verbose: Tracer.log.info("TESTING: Got Exception \n")
控制台输出如下所示:
Running lcov -r /remote/XXXXXX/coverage.19.temp "/remote/XXXXXX/xml/2009a/generated/*" -o /remote/XXXXX/gcov/coverage.19
由于我对python脚本不是很熟悉,所以我只是在闲逛,不管我在这里做错了什么......我怀疑某个地方出现了僵局......
stdout, stderr = process.communicate()
处理这些问题吗?
任何专家回答在哪种情况下subprocess.call会挂起吗? 非常感谢
使用子进程时,我倾向于做这样的事情:
is_running = lambda: my_process.poll() is None
my_process = subprocess.Popen(' '.join(exclude),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
shell=True)
# Grab all the output from stdout and stderr and log it
while is_running():
rlist, wlist, xlist = select.select([my_process.stdout, my_process.stderr], [], [], 1)
# Log stdout, but don't spam the log
if my_process.stdout in rlist and verbose:
# Adjust the number of bytes read however you like, 1024 seems to work
# pretty well for me.
Tracer.log.debug(my_process.stdout.read(1024))
# Log stderr, always
if my_process.stderr in rlist:
# Same as with stdout, adjust the bytes read as needed.
Tracer.log.error(my_process.stderr.read(1024))
我已经看到stdout的东西过去只是在我的日志中转储了一堆空行,这就是为什么我在调试级别记录它。 在开发期间打印到我的日志,但从未在生产中写入,因此我可以安全地将其保留在代码中进行调试,而不会在其日志中添加垃圾。
希望这可以帮助揭示程序挂起的位置以及导致它的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.