gpt4 book ai didi

python - subprocess.popen 的 poll 方法对于长进程返回 None

转载 作者:太空狗 更新时间:2023-10-29 22:23:20 35 4
gpt4 key购买 nike

我正在通过子进程执行 curl 命令。此 curl 命令在另一台服务器上启动视频处理,并等待响应。该过程完成后,远程服务器返回 json 对象。我正在使用 poll() 值检查子进程的状态,该值是 None - 进程未完成,0- 进程成功完成,1- 表示错误。

如果远程服务器上的处理时间大约为 30 分钟或更少,我将得到正确的响应,但如果处理时间更长,我将只得到 None 值,即使我可以看到远程服务器已完成处理并已返回 json 对象。

谁能告诉我,poll() 在特定时间后仅返回 None 的可能原因是什么。提前谢谢你。

我的 Popen 对象是:

object = subprocess.Popen(str(curlCmd), shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

我每 2 秒调用一次 object.poll() 来检查进程是否成功完成。

最佳答案

.poll() is None 表示 child 还在跑

curl 进程可能会在填满其 stdout 或 stderr OS 管道缓冲区(在我的 Linux 机器上约为 64K)后立即挂起,除非您从管道的末端读取数据。

也就是说,当您等待子进程完成时,子进程等待您清空管道缓冲区——死锁。来自 the subprocess docs :

This[.wait()] will deadlock when using stdout=PIPE 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 Popen.communicate() when using pipes to avoid that.

您可以使用线程、async.io 来同时使用管道。

关于python - subprocess.popen 的 poll 方法对于长进程返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11937028/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com