473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

streaming Popen.stdout

I was experimenting with subprocess.Popen with the following script:

$ cat Popen_ex.py
import sys, subprocess
po = subprocess.Popen([sys.executable, 'hello.py'],
stdout=subprocess.PIPE,
bufsize=0)
for line in po.stdout:
print line,

where hello.py is the following script:

$ cat hello.py
import time
time.sleep(1)
print 'hello'
time.sleep(1)
print 'world'
time.sleep(1)
print '*END*'
time.sleep(1)

It turns out that Popen collects all the output and write everything
together
after 4 seconds, where I would like to print a line every seconds.
How can I get that in a simple way? A Unix-only solution would be fine,
too.
Michele Simionato

Feb 21 '06 #1
1 5302
Replying to myself ...

I cooked up this solution involving os.pipe and os.fork, but I am not
especially happy with
it; anyway, let me write it. Feedback is welcome, since this was
written very quickly and
I may have missed something. BTW, are there libraries out there doing
something similar?

----

import subprocess
import os, sys, time

class ReadObject(object):
def __init__(self, fileno):
self.fileno = fileno
self._closed = False
self.name = str(self)
def readline(self):
if self._closed : return ''
return ''.join(iter(self.read1, '\n')) + '\n'
def read(self):
return ''.join(iter(self.read1, '\x00'))
def read1(self):
c = os.read(self.fileno, 1)
if c == '\x00':
self._closed = True
return '\n'
else:
return c
def __iter__(self):
return iter(self.readline, '')

class WriteObject(object):
def __init__(self, fileno):
self.fileno = fileno
self.name = str(self)
def write(self, text):
os.write(self.fileno, text)
def flush(self):
pass
def close(self):
self.write('\x00')

def callproc(child, *args,**kw):
"Run the child procedure in a child process"
r, w = os.pipe()
R, W = ReadObject(r), WriteObject(w)
if os.fork(): # parent
return R
else: # child
sys.stdout = W
try:
child(*args, **kw)
finally:
W.close()
sys.exit()

if __name__ == '__main__':
for line in callproc(subprocess.call, [sys.executable,
'hello.py']):
print line,

Feb 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Tom Brown | last post by:
I need to chain together three linux commands and get the final output. I read the documentation for Popen in the subprocess module for replacing the shell pipe line. I followed the example and...
4
by: jelle | last post by:
Hi, I use python quite a bit to couple different programs together. Doing so has been a _lot_ easier since subprocess came around, but would really like to be able to use the succinct shell...
3
by: madpython | last post by:
playing with subprocess.Popen on Windows I stumbled into the following problem: Python 2.4.3 (#69, Mar 29 2006, 17:35:34) IDLE 1.1.3 >>> import subprocess >>>...
9
by: Clodoaldo Pinto Neto | last post by:
Output from the shell: $ set | grep IFS IFS=$' \t\n' Output from subprocess.Popen(): "IFS=' \t\n" Both outputs for comparison:
13
by: bayer.justin | last post by:
Hi, I am trying to communicate with a subprocess via the subprocess module. Consider the following example: <subprocess.Popen object at 0x729f0> Here hey is immediately print to stdout of...
2
by: Greg Ercolano | last post by:
When I use os.popen(cmd,'w'), I find that under windows, the stdout of the child process disappears, instead of appearing in the DOS window the script is invoked from. eg: C:\type foo.py import...
3
by: Jesse | last post by:
Hi all, I have a problem using wget and Popen. I hope someone can help. -- Problem -- I want to use the command: wget -nv -O "dir/cpan.txt" "http://search.cpan.org" and capture all it's...
8
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK....
5
by: thedsadude | last post by:
Hello, I'm launching a script as follows: <code> p = subprocess.Popen() p.wait() </code> If p.py writes to sys.stdout, then it is shown on the console.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.