Skip to content

Instantly share code, notes, and snippets.

@hemanth
Created June 14, 2013 05:58
Show Gist options
  • Save hemanth/5779767 to your computer and use it in GitHub Desktop.
Save hemanth/5779767 to your computer and use it in GitHub Desktop.
Async process execution in python.
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