473,782 Members | 2,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Non-blocking pipes during subprocess handling

I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it? Currently I'm doing this:

process = subprocess.Pope n(
args,
bufsize=1,
universal_newli nes=True,
stdout=subproce ss.PIPE,
stderr=subproce ss.PIPE)

def ProcessOutput(i nstream, outstream):
text = instream.readli ne()
if len(text) 0:
print >>outstream, text,
return True
else:
return False

while process.poll() is None:
ProcessOutput(p rocess.stdout, sys.stdout)
ProcessOutput(p rocess.stderr, sys.stderr)

# clean up everything to EOF once the process ends.
somethingPrinte d = True
while somethingPrinte d:
somethingPrinte d = ProcessOutput(
process.stdout, sys.stdout)
somethingPrinte d |= ProcessOutput(
process.stderr, sys.stderr)
Unfortunately, stream.readline will block 'til it gets a line, and
typically there won't be anything on the stderr stream. The reason for
the redirections in the first place is that I'm launching this script as
a subprocess from a GUI app that catches stdout and stderr and directs
the output to the appropriate windows, but in some cases I don't
actually want the output at all (I've removed that logic though since it
needlessly complicates my example; suffice to say everything below the
process = subprocess.Pope n... line is enclosed in a try and then in an
if block.

The documentation on file.read() indicate that there's an option for
"non-blocking" mode, but I'm stumped as to how to even look for how to
enable and use that.

thanks,
-tom!

--
Jan 9 '07 #1
3 2986
At Monday 8/1/2007 22:09, Tom Plunket wrote:
>I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it? Currently I'm doing this:
Using a thread for each stream is the safest way, specially if you
can't control the child process.
--
Gabriel Genellina
Softlab SRL


_______________ _______________ _______________ _____
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

Jan 9 '07 #2
Tom Plunket <to***@fancy.or gwrote:
I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it?
You might want to check out this modification to subprocess which does
non-blocking pipes.

http://aspn.activestate.com/ASPN/Coo.../Recipe/440554

I personally think something like that should be built into subprocess

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Jan 9 '07 #3
In article <22************ *************** *****@4ax.com>,
Tom Plunket <to***@fancy.or gwrote:
I'm using subprocess to launch, well, sub-processes, but now I'm
stumbling due to blocking I/O.

Is there a way for me to know that there's data on a pipe, and possibly
how much data is there so I can get it? Currently I'm doing this:

process = subprocess.Pope n(
args,
bufsize=1,
universal_newli nes=True,
stdout=subproce ss.PIPE,
stderr=subproce ss.PIPE)

def ProcessOutput(i nstream, outstream):
text = instream.readli ne()
if len(text) 0:
print >>outstream, text,
return True
else:
return False

I think it would be fair to say that your problem is
not due to blocking I/O, so much as buffered I/O. Since
you don't appear to need to read one line at a time, you
can detect and read data from the file descriptor without
any buffering. Don't mix with buffered I/O, as this will
throw the select off. From memory - better check, since
it has been a while since I wrote anything real like this
(or for that matter much of anything in Python) --
import select
def ProcessOutput(i nstream, outstream):
fdr = [instream.fileno ()]
(r, w, e) = select.select(f dr, [], [], 0.0)
for fd in r:
text = os.read(fd, 4096)
outstream.write (text)

Donn Cave, do**@u.washingt on.edu
Jan 9 '07 #4

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

Similar topics

17
1757
by: Doug Fort | last post by:
This is an excerpt from a much longer post on the python-dev mailing list. I'm responding here, to avoid cluttering up python-dev. <snip> >Some English readers might not really imagine, but it is a constant >misery, having to mangle identifiers while documenting and thinking >in languages other than English, merely because the Python notion of >letter is limited to the English subset. Granted, keywords and standard >library use...
12
4430
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few characters as possible will be matched. the regular expression module fails to perform non-greedy matches as described in the documentation: more than "as few characters as possible"
5
3755
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence their dtor is called immediately and not at the end of the function. to be able to use return objects (to avoid copying) i often assign them to a const reference. now, casting a const return object from a function to a non-const reference to this...
25
7646
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
4527
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
8
3516
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I cannot load/unload/reload extensions into my large and slow-to-load application during development without restarting the process then the disadvantages may outweigh the advantages. I've got a mixed-mode program in which I create a new AppDomain...
14
8472
by: Patrick Kowalzick | last post by:
Dear all, I have an existing piece of code with a struct with some PODs. struct A { int x; int y; };
11
3454
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the vtable pointer is made use of.. eg. class one {
2
6119
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my pointers, but I'm not sure. Please help. The code: void equate(matrix *A, matrix *B) { int i, j; assert(A.row_dim == B.col_dim && A.col_dim == B.col_dim); for(i=0; i < A.row_dim; i++) for(j=0; j < A.col_dim; j++)
399
12939
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or to python-3000@python.org In summary, this PEP proposes to allow non-ASCII letters as identifiers in Python. If the PEP is accepted, the following identifiers would also become valid as class, function, or variable names: Löffelstiel,...
0
9639
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9479
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10146
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8967
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7492
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5509
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3639
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2874
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.