Problem with select.poll and popen2

Magnus Lycka lycka at carmen.se
Tue Aug 30 15:55:02 CEST 2005


I'm trying to read standard out in a process started with popen2
in a non-blocking way. (Other good ways of doing this than the
one I tried are appreciated.)

I've tried to dumb down my code to see what happens, and
socket.poll seems to behave very strangely. I've tried to
use the .poll method for the poll object with and without
a timeout, but in either case, the output randomly switches
between on of the versions below. It runs fast, there is
certainly no 1000 ms delay between printed full stops.

I suspect Python does things in the background with the
pipes that confuse select/poll.

import sys
import select
import popen2
import os

CMD = 'date'

#fi, fo = os.popen2(CMD) # Same result with this...
fo, fi = popen2.popen2(CMD)
p = select.poll()
p.register(fo, select.POLLOUT)
while 1:
     print '.',
     sys.stdout.flush()
     l = p.poll(1000)
     for (o, evt) in l:
	if o == fo.fileno() and evt == select.POLLOUT:
             print 'Reading', fo
             c = fo.read(1)
             print c,

$ python clpy.py
. Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
T . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
u . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
e . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
   . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
A . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
u . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
g . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
   . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
3 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
0 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
   . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
1 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
5 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
: . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
4 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
5 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
: . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
3 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
8 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
   . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
C . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
E . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
S . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
T . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
   . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
2 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
0 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
0 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
5 . Reading <open file '(fdopen)', mode 'r' at 0x92f9070>

. Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
[etc]

$ python clpy.py
. Reading <open file '(fdopen)', mode 'r' at 0x92f9070>
T . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
[etc]

$ python clpy.py
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
[etc]



More information about the Python-list mailing list