Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Reading output from a child process non-blockingly

Reply
Thread Tools

Reading output from a child process non-blockingly

 
 
Yuan HOng
Guest
Posts: n/a
 
      06-29-2005
In my program I have to call an external program and parse its output.
For that I use the os.popen2 function, and then read the output
stream.

But the complexity is that the external program gives back its output
in a piecemeal manner, with long delays between the outputs. In the
main program I want to therefore read the output in a non-blocking
manner, to read as many bytes as the child process is spitting out.

The question is, how can I achieve that?

I tried use select.select on the output stream returned by os.popen2,
but it returns a readable file descriptor only after the whole child
process ends.

Here is a script simulating the external program:

test.py:
import sys, time
print 'hello\n'*500
sys.stdout.flush()
time.sleep(100)
print 'world\n'*500

And here is what I am tring to do in the main program to read its output:

import os, select
cmd = 'python test.py'
pin, pout = os.popen2(cmd)
while not select.select([pout], [], [], some_timeout)[0]:
pass
pout.readline()

I hope to get the first return very soon, before the external program
sleeps, but instead only after the whole program exits do I get any
output.

Can anyone give me a hint?

--
Hong Yuan

$BBg4I2Hf&>e7z:`D6;T(B
www.homemaster.cn
 
Reply With Quote
 
 
 
 
ilochab@gmail.com
Guest
Posts: n/a
 
      06-29-2005


Yuan HOng ha scritto:
> In my program I have to call an external program and parse its output.
> For that I use the os.popen2 function, and then read the output
> stream.
>
> But the complexity is that the external program gives back its output
> in a piecemeal manner, with long delays between the outputs. In the
> main program I want to therefore read the output in a non-blocking
> manner, to read as many bytes as the child process is spitting out.
>
> The question is, how can I achieve that?
>


What about using a thread to control the child process?

Licia

 
Reply With Quote
 
 
 
 
Thomas Guettler
Guest
Posts: n/a
 
      06-29-2005
Am Wed, 29 Jun 2005 16:08:54 +0800 schrieb Yuan HOng:

> In my program I have to call an external program and parse its output.
> For that I use the os.popen2 function, and then read the output
> stream.

[cut]
> I tried use select.select on the output stream returned by os.popen2,
> but it returns a readable file descriptor only after the whole child
> process ends.


Select is the right module for this. But it only works on file descriptors
on unix.

What happens, if you run the external command on the shell like this:

ext_prog > out.log &

Check out.log with "tail" or "less +F". Do you see the data appear
in small chunks? If not, the application detects that stdout is not
a tty and you only get data if the buffer is full or if the application
ends.

If out.log gets filled in chunks you want to read, you can read this file
as long as the application is running. Remember the size of the file after
each read and use fd.seek() to read only the new data after opening it
again. This should work on windows, too.

HTH,
Thomas

--
Thomas Güttler, http://www.thomas-guettler.de/


 
Reply With Quote
 
Dan Sommers
Guest
Posts: n/a
 
      06-29-2005
On Wed, 29 Jun 2005 15:45:20 +0200,
Thomas Guettler <> wrote:

> Check out.log with "tail" or "less +F". Do you see the data appear in
> small chunks? ...


You'll need "tail -f", I think.

Regards,
Dan

--
Dan Sommers
<http://www.tombstonezero.net/dan/>
 
Reply With Quote
 
 
 
Reply

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
pexpect on windows - child process of another child process - quickquestion Z W Python 0 03-09-2013 10:00 PM
Child process wait father process excute some code then begin execute empriser C Programming 1 03-06-2007 02:24 AM
Re: Reading output from a child process non-blockingly Adriaan Renting Python 0 06-30-2005 07:47 AM
(Win32) Timing out a process while reading process' output? rtm Perl 0 09-27-2004 10:06 PM
How do I: Main thread spawn child threads, which child processes...control those child processes? Jeff Rodriguez C Programming 23 12-09-2003 11:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57