您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python子进程调用挂起

Python子进程调用挂起

当使用子过程时,我倾向于做这样的事情:

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东西只是在日志中转储了一堆空行,这就是为什么我在调试级别记录它。那会在开发期间打印到我的日志中,但是永远不会写在生产中,因此我可以安全地将其保留在代码中进行调试,而无需在其日志中放入垃圾

希望这可以帮助您揭露程序的挂起位置以及造成该问题的原因。

python 2022/1/1 18:27:38 有188人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶