【发布时间】:2013-05-07 14:16:21
【问题描述】:
我有一个在 Win 2003 上运行良好的简单代码:
proc = subprocess.Popen('<some python script which runs another process>', stdout = subprocess.PIPE, stderr = subprocess.PIPE, stdin = subprocess.PIPE)
out = proc.communicate()[0]
但是在 Windows 8 上这部分; out = proc.communicate()[0],挂起。
有人看过这个问题吗?
- 我检查了进程是否真的终止了(当子进程启动时 PID 不存在)
- 制作proc.stdout.readlines()也是个问题,也挂了。如何检查标准输出是否有 EOF?
- 当我停止子进程 proc.communicate() 工作正常。
这是最简单的例子:
import subprocess
proc = subprocess.Popen([sys.executable, "D:\\test.py"], stdout = subprocess.PIPE)
print 'PID', proc.pid #When this PID is printed I see in the taskbr that process is already finished
print 'Output', proc.communicate() # but this part is hangs
和代码 od test.py:
import os, time
from subprocess import Popen, PIPE
CREATE_NEW_PROCESS_GROUP = 0x00000200 # note: could get it from subprocess
DETACHED_PROCESS = 0x00000008 # 0x8 | 0x200 == 0x208
p = Popen("start /B notepad", shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE,
creationflags=DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP)
print 'Done'
exit()
这是有效的场景吗?
【问题讨论】:
-
两件事,首先尝试从 windows cmd 手动运行命令,以确保命令正常工作。其次,您是否尝试使用
shell=True标志运行它? -
命令在 cmd 中工作正常,shell 标志并不重要 - 两个标志都不起作用。问题出在子进程中 - 在它运行时 - 尽管父进程已终止,但我无法从父进程获取输出。
-
一个认为你可以做的调试你的程序是在一个while循环中使用
proc.stdout.readline()函数并打印输出行来查看进程搁浅的时间。 -
进程标准输出是否很长(超过 64kb)?因为当进程标准输出超过 64kb 时,我在 windows 中使用
subprocess.PIPE时遇到问题。 -
我也见过... 注意:
subprocess.Popen从最父脚本调用的进程不必是 python 脚本。它也可以是一个批处理文件,它使用start some-process.exe来启动一个后台进程。
标签: python windows subprocess