A non-blocking read on a subprocess.PIPE in Python
fcntl
, select
, asyncproc
won't help in this case.
A reliable way to read a stream without blocking regardless of operating system is to use Queue.get_nowait()
:
import sys
from subprocess import PIPE, Popen
from threading import Thread
try:
from queue import Queue, Empty
except ImportError:
from Queue import Queue, Empty # python 2.x
ON_POSIX = 'posix' in sys.builtin_module_names
def enqueue_output(out, queue):
for line in iter(out.readline, b''):
queue.put(line)
out.close()
p = Popen(['myprogram.exe'], stdout=PIPE, bufsize=1, close_fds=ON_POSIX)
q = Queue()
t = Thread(target=enqueue_output, args=(p.stdout, q))
t.daemon = True # thread dies with the program
t.start()
# ... do other things here
# read line without blocking
try: line = q.get_nowait() # or q.get(timeout=.1)
except Empty:
print('no output yet')
else: # got line
# ... do something with line
Python: How to read stdout of subprocess in a nonblocking way
Check select module
import subprocess
import select
import time
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)
y=select.poll()
y.register(x.stdout,select.POLLIN)
while True:
if y.poll(1):
print x.stdout.readline()
else:
print "nothing here"
time.sleep(1)
EDIT:
Threaded Solution for non posix systems:
import subprocess
from threading import Thread
import time
linebuffer=[]
x=subprocess.Popen(['/bin/bash','-c',"while true; do sleep 5; echo yes; done"],stdout=subprocess.PIPE)
def reader(f,buffer):
while True:
line=f.readline()
if line:
buffer.append(line)
else:
break
t=Thread(target=reader,args=(x.stdout,linebuffer))
t.daemon=True
t.start()
while True:
if linebuffer:
print linebuffer.pop(0)
else:
print "nothing here"
time.sleep(1)
Related Topics
Remap Values in Pandas Column With a Dict, Preserve Nans
How to Split a String into a List
How to Put a Variable'S Value Inside a String
How Does Python'S Super() Work With Multiple Inheritance
Why Does "Pip Install" Inside Python Raise a Syntaxerror
How to Make a Python Script Standalone Executable to Run Without Any Dependency
Why Can't I Call Read() Twice on an Open File
How to Sort a Dictionary by Key
How to Make a Dictionary from Separate Lists of Keys and Values
Pygame Mouse Clicking Detection
Why Doesn't Calling a String Method Do Anything Unless Its Output Is Assigned
Error: Unable to Find Vcvarsall.Bat
How to Parse Xml and Get Instances of a Particular Node Attribute