Python stdout.readline() 卡住

标签 python python-2.7

我在我的 python 脚本中使用:

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

checkcode.exe 返回一个值,或者什么都不返回,然后保持存活

当 checkcode.exe 返回一个值时一切正常,但是当 checkcode.exe 不返回值时,脚本 block 在 result = proc.stdout.readline()

如何解决问题?

最佳答案

您必须确保子进程退出以防止 readline() 阻塞。 readline 等待直到它收到换行符或文件结尾。 EOF 在程序退出时实现。

或者,您可以使用上面评论中提到的非阻塞 I/O。

关于Python stdout.readline() 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24215560/

相关文章:

python - 如何使用 datashader 为图表的节点着色?

python - 如何在 tkinter 中查看 TreeView 的部分区域和水平滚动?

python-3.x - 如何在python中将png图像的目录转换为jpg

python-2.7 - 如何在 matplotlib 中交互式查找和注释局部最大值(峰值)?

python-2.7 - 在python中模拟按钮点击

python - PyCharm/IntelliJ 启用包依赖项检查

python - 尝试在 render_template() (Flask)之后使用访问另一个作用域中的变量?

python - 名称错误 : 'discord' is not defined?

Python tkinter PhotoImage 无法正常工作

python - Tensorflow-gpu 问题(CUDA 运行时错误 : device kernel image is invalid)