Hey Steve,

Well, I've tried flush() before, but I didn't know about the "-u" switch.

Thank you for tinkering on this, I shall post again if I make any progress.

Avi


Steve Holden wrote:
Steve Holden wrote:

Avi Berkovich wrote:

Hey,

I can't make it work, I don't get any data from either stdout nor stderr.
If I send lines and then close the stdin pipe, I may get an exception message from several lines up.


I tried manually reading from the stdout pipe, but it just blocks and hangs no matter what I send over via the stdin pipe.

This behavior isn't presented by the command line interpreter by any chance.

Any suggestions?



Yes: post your code along with messages (if any)!

regards
 Steve


Sorry, for some reason I can no longer find the message you posted the code in -- it didn't get correctly threaded in Mozilla. Anyway, I can't say that I exactly understand what's going on, but here are a couple of observations:

1. The interpreter performs buffering even when running in interactive mode unless it can see a terminal (which here it can't). Hence adding a "-u" to the interpreter command line is useful.

2. There's a problem with your stop condition, which is why the attached modified version sleeps before closing the pipe to the interpreter.

import threading
import sys
import popen2

class Piper(threading.Thread):

   def __init__(self, readPipe, output):
       threading.Thread.__init__(self)

       if not isinstance(readPipe, file):
           raise TypeError, "readPipe parameter must be of File type"
       #if not isinstance(output, file):
       #    raise TypeError, "output parameter must be of File type"

       self.readPipe = readPipe
       self.output = output
       self.toStop = False

   def run(self):
       print "Running"
       while not self.toStop and not self.readPipe.closed:
           read = self.readPipe.readline()
           self.output.write(read)
           self.output.flush()
       print "Stopped"

   def stop(self):
       self.toStop = True


if __name__ == "__main__": r, w = popen2.popen4('c:\\python24\\python.exe -u') piper = Piper(r, sys.stdout) piper.start() w.write("print 'Hello!'\r\n") w.flush() w.write("print 'Goodbye!'\r\n") w.flush() import time; time.sleep(2) w.close() #import time; time.sleep(3) #r.close() piper.stop()

Various other bits and pieces of messing about didn't really yield any useful conclusions, so I pass this on only in the hope that you may be more motivated to follow up now you can actually see some interpreter output. I would have thought the flush() calls would have made output appear one line at a time, but sadly they do not.

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

Reply via email to