[Tutor] thread and os.pipe (was: logic for a tree like structure)
tiagosaboga at terra.com.br
Sat Dec 8 11:35:43 CET 2007
I started a message, changed my mind, wrote about some other thing and
forgot to change the subject. Sorry for the noise.
On Sat, Dec 08, 2007 at 07:25:16AM -0200, Tiago Saboga wrote:
> I need an additional thread in my app, and I am trying to understand
> how it works. Alas, all I found about threads is too simple (does not
> fit in my real life problem) or too complicated. The best thing I've
> read until now is the great tutorial by Norman Matloff, but it isn't
> enough. If you could point me to some more docs, it would be great.
> The python docs have been of no use for me in this case.
> Anyway, I am trying to play a little with it, and I can't understand
> what's happening in this simple example. I want to establish a
> connection between my two threads by a os.pipe, but it seems like I
> can only read the pipe when everything was done. I was hoping to see
> the output of writer.py come out in real time, but it is coming out
> all together when writer.py returns. Why?
> Tiago Saboga.
> import time
> for i in range(10):
> print 'Line number %s.' % i
> from __future__ import with_statement
> import thread
> import subprocess
> import os
> def run(out):
> subprocess.Popen(['./writer.py'], stdout=os.fdopen(out, 'w'))
> def main():
> out_r, out_w = os.pipe()
> thread.start_new_thread(run, (out_w,))
> with os.fdopen(out_r) as f:
> while True:
> if line:
> print '[main]', line
> if __name__=='__main__':
> Tutor maillist - Tutor at python.org
More information about the Tutor