簡體   English   中英

Python子進程調用掛起

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM