473,398 Members | 2,343 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,398 software developers and data experts.

Problem with select.poll and popen2

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]
Aug 30 '05 #1
1 2553
Magnus Lycka wrote:
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'm starting to get on top of this. First of all, I was confused
by POLLIN and POLLOUT, since it's IN or OUT of the polling process,
so we want the POLLIN filter for the output of the spawned process...

Secondly, I thought that I'd get a fd, event pair for each kind of
event, but events are bitwise ORed together, and in my dumb downed
version I had used a command (date) that ends quickly, and then there
is a stream of POLLHUP that comes out of poll, even though I didn't
register for that. (Is it supposed to be like that?)

The following pair of programs (sender.py first) seems to behave as
I intended. Perhaps they can be of use for someone. After all, it's
not only sockets that we might want to read in an unblocking way,
but it seems most examples are geared towards that.
import time, sys
while 1:
print 'HELLO'
sys.stdout.flush()
time.sleep(2)

.....

import sys
import select
import os

CMD = 'python sender.py'

fi, fo = os.popen2(CMD)
fi.close()
p = select.poll()
p.register(fo, select.POLLIN)
while 1:
l = p.poll(500)
for (o, evt) in l:
if o == fo.fileno() and evt & select.POLLIN:
c = fo.read(10)
print c,
else:
print "Timeout!"

Aug 30 '05 #2

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

Similar topics

1
by: A. Lloyd Flanagan | last post by:
OK, I've got a weird one I haven't been able to figure out yet. Admittedly I haven't had time to dig into the library source, but this behavior certainly doesn't seem right. Here's a test case: ...
0
by: Jim | last post by:
Hi, I noticed with popen2.Popen4 class, sometimes the poll() method will keep returning -1 although the command has finished. However, when a read is done ( ex. p4instance.fromchild.read() )...
1
by: Chris S. | last post by:
A wrote a small class to handle IO through pipes, but the connection only seems to work in one direction. The following code defines connection.py, engine.py, and controller.py. Once connected, the...
3
by: rh0dium | last post by:
Hi all, Another newbie question. So you can't use signals on threads but you can use select. The reason I want to do this in the first place it I need a timeout. Fundamentally I want to run a...
2
by: Xah Lee | last post by:
Python Doc Problem Example: os.system Xah Lee, 2005-09 today i'm trying to use Python to call shell commands. e.g. in Perl something like output=qx(ls) in Python i quickly located the...
1
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
1
by: diego | last post by:
I'm trying to understand how popen2 works. Found in this group, that popen2.popen2 can cause trouble so i chose win32pipe.popen2. have a look a the listing of 2 files: ekmain.py: **************...
5
by: JamesHoward | last post by:
I have a problem with python's asyncore module throwing a bad file descriptor error. The code might be difficult to copy here, but the problem is essentially: The server wants to sever the...
1
by: shapper | last post by:
Hello, I have two tables: Polls PollID, Question Options OptionID, Answer Given a OptionID I want to get the poll to which the Option "Belongs": Poll poll = database.Polls.Select(p...
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: 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:
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.