简体   繁体   English

subprocess.Popen卡住的脚本执行

[英]subprocess.Popen execution of a script stuck

I am trying to execute a command as follows but it is STUCK in try block as below until the timeout kicks in,the python script executes fine by itself independently,can anyone suggest why is it so and how to debug this? 我正在尝试执行以下命令,但在下面的try块中是STUCK,直到超时开始,python脚本本身独立执行良好,有人可以建议为什么这样做以及如何调试吗?

cmd = "python complete.py"
proc = subprocess.Popen(cmd.split(' '),stdout=subprocess.PIPE )
print "Executing %s"%cmd
try:
    print "In try" **//Stuck here**
    proc.wait(timeout=time_out)
except TimeoutExpired as e:
    print e
    proc.kill()
with proc.stdout as stdout:
    for line in stdout:
        print line,

proc.stdout isn't available to be read after the process exits . 进程退出后 proc.stdout无法读取。 Instead, you need to read it while the process is running . 相反,您需要在进程运行时阅读它。 communicate() will do that for you, but since you're not using it, you get to do it yourself. communicate()将为您完成此操作,但是由于您没有使用它,您可以自己进行操作。

Right now, your process is almost certainly hanging trying to write to its stdout -- which it can't do, because the other end of the pipe isn't being read from. 现在,几乎可以肯定,尝试写入其stdout的过程已经挂起了–这是做不到的,因为未从管道的另一端读取该信息。

See also Using module 'subprocess' with timeout . 另请参见将模块'subprocess'与timeout一起使用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM