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.
----- Original Message -----
Sent: Tuesday, March 08, 2005 12:03 AM
Subject: Queued stdout, flush() doesn't seems to help...

Hello Everyone,
 
I have been using this piece of code to start an exec and "process" its stdout/stderror
 
import sys
import popen2
executable = r'execfile -arg1 -arg2'
r, w, e = popen2.popen3(executable)
r.flush()
w.flush()
e.flush()
x=0
# just print the 1000 first lines
while x<1000:
    sys.stdout.write(e.readline())
    sys.stdout.flush()
    sys.stdout.write(r.readline())
    sys.stdout.flush()
 
whenever running this code, the process starts up fine, but no print occurs as the exec is running.
Somehow, the print results shows up only when the process is stopped or when resuming ( thru the windows task list).
 
is there a way to get the print to show its results as as it is trapped by open3() function ( namely realtime printing ).
 
Does someone would have a solution for this problem?
Tx in advance for your help.
 
Dave.
 


--
http://mail.python.org/mailman/listinfo/python-list
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to