Created
June 14, 2013 03:03
-
-
Save hemanth/5779187 to your computer and use it in GitHub Desktop.
Async subprocess execution in python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 3.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() | |
def execute(cmd,path): | |
p = Popen(cmd, stdout=PIPE, bufsize=1, close_fds=ON_POSIX, shell=True, cwd=path) | |
q = Queue() | |
t = Thread(target=enqueue_output, args=(p.stdout, q)) | |
t.daemon = True # thread dies with the program | |
t.start() | |
# read line without blocking | |
try: line = q.get_nowait() # or q.get(timeout=.1) | |
except Empty: | |
print('no output yet') | |
else: | |
print line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment