[Tutor] thread and os.pipe (was: logic for a tree like structure)

Alan Gauld alan.gauld at btinternet.com
Sat Dec 8 12:54:07 CET 2007


"Tiago Saboga" <tiagosaboga at terra.com.br> wrote

> what's happening in this simple example. I want to establish a
> connection between my two threads by a os.pipe,

While its possible to use a pipe to communicate within a process
its not very helpful and very rarely done. Usially queues are used
for communication between threads

> I was hoping to see the output of writer.py come out in real time

But you aren't really using a thrwad in your code you are
spawning a new subprocess using Popen and reading the
output of that from a pipe. are you sure you really need to
do that in a thread? You can simply dip into the pipe and
read it on demand. The usual case for a thread is when
you want to perform two tasks concurrently within the same
process, not to run another parallel process.,

> ... but it is coming out all together when writer.py returns. Why?

Dunno, it depends on what writer.py is doing. If its writing
to stdout then the puipe should be able to read the rewsults
as you go.

>> 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,))

Why not just set up outw to the output of Popen?
Using the subprocess pattern:

pipe = Popen(cmd, shell=True, bufsize=bufsize, stdout=PIPE).stdout

or in your case:

f = subprocess.Popen(['./writer.py'], shell=True, 
stdout=subprocess.PIPE).stdout


>>     with os.fdopen(out_r) as f:

So you don't need this

>>         while True:
>>             line=f.readline()
>>             if line:
>>                 print '[main]', line

And this should just work.

And if you really want to do something else wehile the output of 
writer
is being collected put all of the above into your run function.

Its not clear to me that you really need a thread here? A simpler
approach may work better.

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list