How to catch a line with Popen

Chris Torek nospam at torek.net
Mon May 30 17:33:16 EDT 2011


>Chris Torek wrote:
>> In at least some versions of Python 2
[the "file"-type object iterators behave badly with pipes]

(This may still be true in Python 3, I just have no experience with
Py3k.  "At least some version of Python 2" means "the ones I have
access to, and have tried." :-) )

In article <is0d44$d7m$1 at speranza.aioe.org>
TheSaint  <nobody at nowhere.net.no> wrote:
>I'm with P3k :P. However thank you for your guidelines.
>Last my attempt was to use a *for* p.wait() , as mentioned earlier

If you have a process that has not yet terminated and that you
must stop from your own python program, calling the wait() method
will wait forever (because you are now waiting for yourself, in
effect -- waiting for yourself to terminate the other process).

The only time to call p.wait() (or p.communicate(), which itself
calls the wait() method) is when you believe the subprocess is
on its wao to terminating -- in this case, after you force it
to do so.

>That looks good enough. I noted some little delay for the first lines, 
>mostly sure Popen assign some buffer even it is not set.

According to the documentation, the default buffer size of Python 2
is 0, which is passed to fdopen() and makes the resulting files
unbuffered.  I recall some sort of changes discussed for Py3k though.

>Haven't you try a perpetual ping, how would be the line_at_a_time ?

Since it is a generator that only requests another line when called,
it should be fine.  (Compare to the itertools "cycle" and "repeat"
generators, for instance, which "return" an infinite sequence.)
-- 
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W)  +1 801 277 2603
email: gmail (figure it out)      http://web.torek.net/torek/index.html



More information about the Python-list mailing list