[Tutor] logic for a tree like structure
Tiago Saboga
tiagosaboga at terra.com.br
Sat Dec 8 10:25:16 CET 2007
Hi,
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?
Thanks,
Tiago Saboga.
=====writer.py=======
#!/usr/bin/python2.5
import time
for i in range(10):
print 'Line number %s.' % i
time.sleep(1)
======threadit.py=======
#!/usr/bin/python2.5
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:
line=f.readline()
if line:
print '[main]', line
if __name__=='__main__':
main()
More information about the Tutor
mailing list