使用Popen.wait()时遇到死锁

2024-09-30 12:17:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我们需要使用subprocess.Popen()执行一个系统命令

由于输出有大量数据,当我们试图使用Popen.wait()时,它被卡住了

pipe= subprocess.Popen(cmd, shell=False,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
pipe.wait()

因此,我们尝试将Popen.stdout放入变量pstdout。之后pstdout被关闭,我们无法获取该文件中的内容

pipe.stdout.closed
pstdout.closed

有人能帮助我们避免挂起或重新打开pstdout


Tags: 文件数据cmdfalsestderrstdoutshellsubprocess
1条回答
网友
1楼 · 发布于 2024-09-30 12:17:58

引用函数Popen.wait()上的python文档popen

Warning This will deadlock when using stdout=PIPE and/or stderr=PIPE and the child process generates enough output to a pipe such that it blocks waiting for the OS pipe buffer to accept more data. Use communicate() to avoid that.

也许你会陷入僵局。用交流代替

p= subprocess.Popen(cmd,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.communicate()

相关问题 更多 >

    热门问题