python subprocess.Popen hanging(python subprocess.Popen 挂起)
问题描述
child = subprocess.Popen(command,
shell=True,
env=environment,
close_fds=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
bufsize=1,
)
subout = ""
with child.stdout:
for line in iter(child.stdout.readline, b''):
subout += line
logging.info(subout)
rc = child.wait()
有时(间歇性地)这会永远挂起.不确定它是否挂在 iter(child.stdout.readline) 或 child.wait()
some times (intermittently) this hangs forever.
not sure if it hangs on iter(child.stdout.readline) or child.wait()
i ps -ef 用于它弹出的进程并且该进程不再存在
i ps -ef for the process it Popens and that process no longer exists
我的猜测是它与 bufsize 有关,因此 child.stdout.readline 会永远持续,但我不知道如何测试它,因为这种情况会间歇性发生
my guess is that it has do with bufsize so that child.stdout.readline is going on forever but i have no idea how to test it and as this happens intermittently
我可以实现警报,但我不确定这是否合适,因为我无法真正判断 popend 进程是缓慢还是挂起
I could implement alarm but i m not sure if that's appropriate as i cant really tell whether the popen'd process is just slow or hanging
假设 child.stdout.readline 或 wait() 永远挂起,除了警报我还能采取什么行动?
let's assume that either child.stdout.readline or wait() hangs forever, what actions could i take besides alarm ?
推荐答案
你很可能遇到了 文档中有解释:
Popen.wait():
等待子进程终止.设置并返回 returncode 属性.
Wait for child process to terminate. Set and return returncode attribute.
警告:这将在使用 stdout=PIPE 和/或 stderr=PIPE 时出现死锁,并且子进程会向管道生成足够的输出这样它会阻止等待操作系统管道缓冲区接受更多数据.使用 communicate() 来避免这种情况.
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.
解决方案是使用 Popen.communicate().
The solution is to use Popen.communicate().
这篇关于python subprocess.Popen 挂起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python subprocess.Popen 挂起
基础教程推荐
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
