简体   繁体   English

Python stdout.readline()冻结

[英]Python stdout.readline() freeze

i'm using in my python script: 我在我的python脚本中使用:

cmd = ["checkcode.exe", "code=1234"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, close)
result = proc.stdout.readline()

checkcode.exe return a value, or nothing, and stay alive checkcode.exe返回一个值,或什么也没有,并保持活着

All works fine when checkcode.exe return a value, but when checkcode.exe don't return value, the script block at result = proc.stdout.readline() 当checkcode.exe返回值时,一切正常,但是当checkcode.exe没有返回值时,结果= proc.stdout.readline()处的脚本块

How to resolve the problem? 如何解决问题?

You must ensure that the subprocess exits in order to prevent readline() from blocking. 您必须确保子进程退出,以防止readline()被阻塞。 The readline waits until it receives a newline or end-of-file. readline会一直等到收到换行符或文件结尾。 EOF is achieved on program exit. EOF是在程序退出时实现的。

Alternatively, you can use non-blocking I/O as mentioned in the comment above. 或者,您可以使用上面注释中提到的非阻塞I / O.

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

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