Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

subprocess readline blocking problem

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
Comment

PREVIOUS NEXT
Code Example
Python :: Python - Create a text border with dynamic size 
Python :: buscar elemento en lista python 
Python :: variance in machine learning 
Python :: read the entire images in the dataset 
Python :: scaling, cross validation and fitting a model through a pipline 
Python :: python print install directory 
Python :: custom point annotation pyplot scatter 
Python :: newton backward interpolation python code 
Python :: Horizontal stacked percent bar chart - with dataframe, seaborn colormap 
Python :: create horizontal descriptives table pandas 
Python :: python setup specify c++ version 
Python :: python join multiple strings ignore none and empty string 
Python :: huffepuf 
Python :: give colour to the font in python email message 
Python :: nn.softmax for pure sconvoultional classifier 
Python :: if user_answer==answer: ecpeted index erroe pythin fx 
Python :: addDataToExp() psychopy 
Python :: access nested set with array params python 
Python :: Python Script to check how many images are broken 
Python :: python how to d oa hello worl 
Python :: pandas iloc include header 
Python :: work day prior to date python 
Python :: FilePathField 
Python :: Returns the cartesian product with another DataFrame 
Python :: copy constructor python 
Python :: github/hacksofteare 
Python :: api csv python 
Python :: print python setencode 
Python :: how to make a number guessing game in python 
Python :: python string match http 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =