5

Python バージョン: 2.6.7 18 回実行される for ループ内に次の subprocess.call がありますが、プロセスは 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 がハングするかについての専門家の回答はありますか? どうもありがとう

4

1 に答える 1

4

サブプロセスを使用するときは、次のようなことをする傾向があります。

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 がログに空の行を大量にダンプするのを見たことがあります。これが、デバッグ レベルでログに記録する理由です。これは開発中にログに出力されますが、本番環境では書き込まれないため、ログにゴミを入れることなくデバッグ用にコードに安全に残すことができます.

願わくば、これが、プログラムがハングしている場所とその原因を明らかにするのに役立つことを願っています。

于 2013-08-16T13:50:17.283 に答える