Python子进程调用挂起

Python subprocess call hangs(Python子进程调用挂起)
本文介绍了Python子进程调用挂起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 版本:2.6.7我在执行 18 次的 for 循环中有以下 subprocess.call,但是,该进程在第 19 次循环中不断挂起:

Python version: 2.6.7 I have the following subprocess.call within a for loop which is exectuted 18 times, however, the process constantly hangs on the 19th loop:

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 
") 

控制台输出如下:

Running lcov -r /remote/XXXXXX/coverage.19.temp "/remote/XXXXXX/xml/2009a/generated/*" -o /remote/XXXXX/gcov/coverage.19

由于我对python脚本不是很熟悉,所以我只是在这里徘徊是否我做错了什么......我怀疑某个地方出现了死锁......

Since I am not very familiar with python scripts, I just wandered whether I am doing something wrong here...I suspect a deadlock somewhere..

stdout,stderr = process.communicate()会处理这些问题吗?

请专家解答 subprocess.call 在哪些情况下会挂起?非常感谢

Any expert answer on in which cases would the subprocess.call hang please? Thanks very much

推荐答案

在使用子进程时,我倾向于这样做:

When using subprocess, I tend to do something like this:

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))

过去,我看到标准输出的东西只是在我的日志中转储了一堆空行,这就是我在调试级别记录的原因.这会在开发过程中打印到我的日志中,但从未在生产中写入,因此我可以安全地将其留在代码中进行调试,而不会将垃圾放入日志中.

I've seen the stdout stuff just dump a bunch of empty lines in my logs in the past, which is why I log that at the debug level. That prints to my logs during the development, but never gets written in production, so I can safely leave it in the code for debugging without putting garbage in their logs.

希望这有助于揭示您的程序挂起的位置以及导致挂起的原因.

Hopefully, this can help expose just where your program is hanging and what's causing it.

这篇关于Python子进程调用挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)