Here is the solution, as an fyi
might not be the most elegant solution, but it
works ;)
from subprocess import *
import sys, threading, time # if someone knows who to return value from thread
function,
# pls let me know, I hate to use global variable
;)
def ThreadProcess():
executable = r'exec_file -arg1 -arg2' global p p = Popen(executable, shell=True, bufsize=0, stdout=PIPE, stderr=STDOUT) p.wait() def PrintThread():
global p global myThread # I feel this is a big
hack
# basically, I could not
find file method that stops on "\n" instead of "\0"
# so I have wrote a
while loop to trap "\n" manually.
# pls if someone knows
to do this elegantly, pls let me know, tx
myStr = "" while myThread.isAlive() == True: myChar = p.stdout.read(1) if myChar == '\0': print "Found EOF, exiting!" break myStr = myStr + myChar if myChar == '\n': print myStr myStr = "" continue print "Process Thread exited, so print thread must terminate now!" print "Starting Process thread" myThread = threading.Thread( target=ThreadProcess, args=() ) myThread.start() print "Starting printing thread"
myPrintThread = threading.Thread(target=PrintThread, args=() ) myPrintThread.start() Dave.
|
-- http://mail.python.org/mailman/listinfo/python-list