[Tutor] thread and os.pipe

Tiago Saboga tiagosaboga at terra.com.br
Sat Dec 8 23:03:19 CET 2007

On Sat, Dec 08, 2007 at 11:54:07AM -0000, Alan Gauld wrote:
> "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 find - and while googling I discovered I am not alone - very
difficult to grasp all details of thread programming. I am trying to
keep it simple, it's why I have chosen this way. 

> > 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? 

This was just a sandbox; I am trying to make something very simple
work before applying it to my program.

> 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.

The writer.py file was included in my mail. ;)
It just prints out a test line every few seconds. As I said, it's just
a test.

> >> 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?

Because in the more complicated case, the run function would have
several steps, including some external processes with Popen. The run()
function will take care of determining which processes will run,
according to the returncode of the first process), and will have to do
some cleaning in the end. But as this happens, I want my my main()
function to analyse and print the output from the Popen while the
external processes are running. This is what I wanted in this simple
test. I am sorry if I am not clear enough, I am still learning both
python and english!

As for the real program I am making, it's a gui for ffmpeg and several
other little programs to convert video files. The gui is written in a
single class, subclassing the qt-designer/pyuic generated one. I am trying
to make my two auxiliary classes totally independent, as I would love
to make a curses version later. So I have a Converter class which does
the actual job of calling the back-end, but I want the gui to be
updated to show the output from the commands. What I want to do
is make the doit() method of the Converter class return a pipe,
through which it will then send the output of the programs. 

Thanks for your answer, as for the excellent tutorial.


More information about the Tutor mailing list