Created
June 14, 2013 05:58
-
-
Save hemanth/5779767 to your computer and use it in GitHub Desktop.
Async process 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
from subprocess import Popen, PIPE | |
from thread import start_new_thread | |
import os, distutils.spawn | |
def which(cmd): | |
path = distutils.spawn.find_executable(cmd) | |
if path == "None": | |
raise Exception("DepMissing") | |
else: | |
return path | |
def execute(cmd,WorkingDir): | |
try: | |
print "Executing {0}".format(' '.join(cmd)) | |
cmd_path = which(cmd[0]) | |
cmd_args = cmd[1] | |
proc = Popen(cmd_path+cmd_args, cwd=WorkingDir, shell=True, stdout=PIPE, stderr=PIPE) | |
start_new_thread(handle_stderr, (proc.stdout,)) | |
except Exception as e: | |
print "Is {0} installed and in PATH?".format(cmd[0]) | |
def handle_stderr(stdout): | |
while True: | |
data = os.read(stdout.fileno(), 128*1024) | |
if data != "": | |
print(data) | |
else: | |
print "Done!" | |
stdout.close() | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment