I am using a subprocess to start and connect to its output stream (standout). I want to be able to execute, reads non-intercept at its stdout. What is the way to make it? Redline infer detection or to check if I have data on the stream before calling .readline I want to work it under portable or at least Windows and Linux
Here is how I do it for now (avaible if it is blocked at .readline ):
P = subprocess.Popen ('myprogram.exe', stdout = subprocess.PIPE) output_str = p.stdout.readline ()
, will not help in this case.
A reliable way to stream any blocking block is to use the operating system:
import import from imports imports, try the popin thread from threading import : Exclude from the import line in the queue, except the import, etc.: line from queue import, empty # dragon3 x ON_POSIX = sys.builtin_module_names def enqueue_output (out, queue) in: iter in line for (out.readline, b ' '): Queue.put (line) out.close () p = popen ([' myprogram .exe '], stdout = PIPE, bufsize = 1, close_fds = ON_POSIX) q = punk T (=) T = Thread (target = enqueue_output, args = (p.stdout, q)) TD Aemon = True # thread program t.start () dies with # # other things here without any block Try to leave: Except line = q.get_nowait () # or q.get (timeout = .1): print ('no output yet') and: # line found ... with row Do something
Comments
Post a Comment