Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Interprocess communication

Thread Tools

Interprocess communication

Posts: n/a
Hi Python Gurus,

I am writing a GUI app (on linux) using pygtk which would launch some
external applications and display their stdout and stderr inside the
output window of my application synchronously. I am using the
subprocess module's Popen to launch the external programs and to
capture their stdout and stderr. The problem is that, for some
external programs that I launch inside my interface, I am not able to
capture and display the stdout as the program *runs*.

After some investigation, I found out that this problem had nothing to
do with my GUI app not getting refreshed and I was able to reproduce
this problem with normal python scripts. Here is one such script

from subprocess import Popen
from subprocess import PIPE
import sys
if __name__ == '__main__':
prog = sys.argv[1]
proc = Popen(prog, shell = True, stdout = PIPE, bufsize = 0)
out = proc.stdout
while proc.poll() is None:
print out.readline()

Run this program as follows
$ "ping -c 10"

Now, you would see the responses as if you just launched ping on the
command line. Now, lets look at another program. Here is a simple C++
program that prints numbers 1 to 10 at the passage of every second
(sort of a stopwatch)

#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
main ( )
timeval t1, t2;
gettimeofday(&t1, NULL);
int prev = -1;
int cur = 0;
while (true)
if(t2.tv_sec - t1.tv_sec > 10)
cur = t2.tv_sec-t1.tv_sec;
if (cur != prev)
prev = cur;

if you would build this program and call it lets say timer ($ g++ -o
timer timer.cpp) and run it with our python script like this

$python "./timer"

you would see that every time you run the program your results vary
and on top of this the stdout of the timer program gets displayed all
at once presumably when the timer program has completed execution.

Why this discrepancy between the ping and timer programs? Is my script correct? Is there a better or a preferred method for
doing interprocess communication in Python.


Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Approaches of interprocess communication exhuma.twn Python 23 02-18-2007 02:33 PM
Interprocess communication on multi-user machine Michael Butscher Python 7 07-01-2006 10:49 AM
Interprocess communication and memory mapping James Aguilar Python 3 12-20-2005 08:48 AM
SystemVerilog Interprocess Communication - Project VeriPage Update Swapnajit Mittra VHDL 0 12-21-2004 05:11 PM
newbie question: interprocess communication Dave Bartlett ASP .Net 1 05-13-2004 01:27 AM