python:非阻塞子进程,检查stdout [英] python: nonblocking subprocess, check stdout
问题描述
好,所以我要解决的问题是这样:
Ok so the problem I'm trying to solve is this:
我需要运行一个设置了一些标志的程序,检查其进度并报告给服务器.因此,我需要我的脚本来避免在程序执行时阻塞,但是我还需要能够读取输出.不幸的是,我认为Popen可用的任何方法都不会读取输出而不会阻塞.我尝试了以下方法,这有点hacky(允许我们从两个不同的对象读取和写入同一文件吗?)
I need to run a program with some flags set, check on its progress and report back to a server. So I need my script to avoid blocking while the program executes, but I also need to be able to read the output. Unfortunately, I don't think any of the methods available from Popen will read the output without blocking. I tried the following, which is a bit hack-y (are we allowed to read and write to the same file from two different objects?)
import time
import subprocess
from subprocess import *
with open("stdout.txt", "wb") as outf:
with open("stderr.txt", "wb") as errf:
command = ['Path\\To\\Program.exe', 'para', 'met', 'ers']
p = subprocess.Popen(command, stdout=outf, stderr=errf)
isdone = False
while not isdone :
with open("stdout.txt", "rb") as readoutf: #this feels wrong
for line in readoutf:
print(line)
print("waiting...\\r\\n")
if(p.poll() != None) :
done = True
time.sleep(1)
output = p.communicate()[0]
print(output)
不幸的是,直到命令终止后,Popen才似乎不会写入我的文件.
Unfortunately, Popen doesn't seem to write to my file until after the command terminates.
有人知道这样做的方法吗?我不是专用于使用python,但是我确实需要使用相同的脚本将POST请求发送到服务器,因此python似乎比shell脚本更容易选择.
Does anyone know of a way to do this? I'm not dedicated to using python, but I do need to send POST requests to a server in the same script, so python seemed like an easier choice than, say, shell scripting.
谢谢! 会
推荐答案
基本上,您有3个选择:
Basically you have 3 options:
- 使用
threading
读入另一个线程而不会阻塞主线程.
在标准输出上使用 -
select
代替stderrcommunicate
.这样,您可以在数据可用时立即读取数据,并避免阻塞. - 让图书馆解决这个问题,
twisted
是显而易见的选择.
- Use
threading
to read in another thread without blocking the main thread. select
on stdout, stderr instead ofcommunicate
. This way you can read just when data is available and avoid blocking.- Let a library solve this,
twisted
is a obvious choice.
这篇关于python:非阻塞子进程,检查stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!