Python子进程永远不会退出并不断在输出中给出空字符串
所以,我一直面临着在我正在编写的 python 应用程序中使用子进程的问题。为了说明这个问题,我编写了这个小脚本,它很好地复制了我的问题。
from __future__ import print_function
import subprocess as sp
from select import select
p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()
while p.returncode is None or p.stdout.closed or p.stderr.closed:
# print('returncode is', p.returncode)
available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
for r in available_readers:
print(r.read(1))
# output_display.insert(tk.END, r.read(1))
当然,我们都知道 ls
命令在将一些内容打印到 stdout
(或者可能是 stderr
)后立即存在,但上面的脚本从来不存在。
正如您从上面脚本的最后一行(注释)中看到的,我必须将子进程中的内容放入 tk 文本组件中。因此,我无法使用 .communicate
等方法和其他阻塞调用,因为我需要运行的命令需要很长时间,并且我需要(几乎)实时显示输出。 (当然,运行 Tk 时我必须在单独的线程中运行它,但那是另一回事)。
所以,我无法理解为什么这个脚本永远不会退出。它会永远打印空字符串(在 ls 命令的预期输出之后)。
请指教。我在 ubuntu 10.10 上运行 python 2.6.6
编辑:这是上述脚本的有效版本
from __future__ import print_function
import subprocess as sp
from select import select
p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()
while p.poll() is None:
# print('returncode is', p.returncode)
available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
for r in available_readers:
print(r.read(1), end='')
# output_display.insert(tk.END, r.read(1))
print(p.stdout.read(), end='')
print(p.stderr.read(), end='')
So, I have been facing a problem with using subprocess for a python app i am writing. To illustrate the problem, I wrote this small script that replicates my problem pretty well.
from __future__ import print_function
import subprocess as sp
from select import select
p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()
while p.returncode is None or p.stdout.closed or p.stderr.closed:
# print('returncode is', p.returncode)
available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
for r in available_readers:
print(r.read(1))
# output_display.insert(tk.END, r.read(1))
Ofcourse, we all know that ls
command exists immediately after printing some stuff to stdout
(or may be stderr
), but the above script never exists.
As you can see from the last line (comment) in the above script, I have to put the content from the subprocess into a tk text component. So, I can't use methods like .communicate
and other blocking calls as the command I need to run takes a long time and I need to show the output (almost) realtime. (Ofcourse, I have to run this in a separate thread when running Tk, but that's something else).
So, I am unable to understand why this script never exits. It keeps printing empty strings forever (after the expected output of the ls
command).
Please advise. I am running python 2.6.6 on ubuntu 10.10
Edit: Here's the version of the above script that works
from __future__ import print_function
import subprocess as sp
from select import select
p = sp.Popen(['ls'], stdout=sp.PIPE, stderr=sp.PIPE, stdin=sp.PIPE)
p.stdin.close()
while p.poll() is None:
# print('returncode is', p.returncode)
available_readers = select([p.stdout, p.stderr], [], [], 2.0)[0]
for r in available_readers:
print(r.read(1), end='')
# output_display.insert(tk.END, r.read(1))
print(p.stdout.read(), end='')
print(p.stderr.read(), end='')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当任何条件为真时循环。您可能只想检查
returncode
(并在每次迭代中检查poll
)。loops while any of the conditions is true. You probably meant to check just
returncode
(andpoll
in every iteration).