473,657 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(C MD)
p = select.poll()
p.register(fo, select.POLLOUT)
while 1:
print '.',
sys.stdout.flus h()
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 2569
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.flus h()
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
1708
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: """Test program to demonstrate problem with popen2 module.""" import popen2 def main (argv): mycmd = 'python2.3 -c "for i in range(100000):\n print i"' p = popen2.Popen3(mycmd)
0
1339
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() ) and then a poll is done, poll() will return the process exit value. Why does this happen? perhaps I have to do some type of flush before doing poll?
1
2292
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 engine is only able to send and the controller recieve. Also, this only works when using popen2.popen3. Using os.popen3 doesn't work at all and seems to behave completely differently. What could be causing these problems? Any help is greatly...
3
2490
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 command on another machine, but I need a timeout. I have to do this to a LOT of machines ( > 3000 ) and threading becomes necessary for timeliess. So I created a function which works with signals ( until you throw threading at it.. ) but I...
2
4542
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 the function due to its
1
2474
by: kristian.hermansen | last post by:
keherman@ibmlnx20:/tmp$ cat helloworld.py #!/usr/bin/env python import pygtk pygtk.require('2.0')
1
1794
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: ************** import win32pipe (stdin1, stdout1) = win32pipe.popen2("test1.py") stdin1.write("1\n")
5
7098
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 connection of an open Asyncore socket. Calling the socket.close() nor the socket.shutdown(2) calls seem to work. The only way I can close the connection without creating the error below is to have the client close the connection. I have the...
1
3037
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 =p.Options.Select(o => o.OptionID = id)).SingleOrDefault;
0
8312
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
8827
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
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...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
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...
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.