select() problem, not timing out

D

D. Shifflett

Hi all,
I am having trouble with a program that ran fine on

Python 2.0 (#0, Mar 1 2001, 01:47:55)
[GCC 2.95.1 19990816 (release)] on linux2

but will not work on

Python 2.3.2 (#1, Oct 8 2003, 17:33:47)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2

These are part of Familiar Linux running on a iPAQ.

The program has a button to cause a packet to be sent to a server,
the program also has a thread to read packets from the server.
I send a packet, I get one back, pretty simple.

I am using select() to wait for packets
and it isn't functioning as expected.

select() doesn't return until I have sent a packet
even though I am using a short timeout (1 second)

So whats happening is I send a packet,
select() returns but I have no input yet,
then I have to send a second packet
then select returns and I can read the
response to the first packet.

Here's a snippet of the code

from sys import argv
from gtk import *
import socket
import time
import threading
import select
....
my_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_sock.bind(('', MY_PORT))

class readerthread(threading.Thread):
def __init__(self):
self._stopevent = threading.Event()
print "thread init"
threading.Thread.__init__(self, name="readerthread")

def run(self):
ilist = []
ilist.append(my_sock)
print "readerthread - my_sock %d" % my_sock.fileno()
while not self._stopevent.isSet():

print "about to select"
il,ol,el = select(ilist,[],[],1)

# read from the socket, etc
if il != []:
data, addr = my_sock.recvfrom(1024)
print "recv() Data length %d" %len(data)
print "recv() Data %s" % data
else:
print "No input from select"

def join(self,timeout=None):
"""
Stop the thread
"""
self._stopevent.set()
threading.Thread.join(self, timeout)
....
def button_cb(button):
my_sock.sendto(data, (SERVERADDR, SERVERPORT))
....

I first posted this problem back on March 11.
I thought it was due to switching to a Win XP system,
now it seems to be due to switch Python versions.

I have tried all the sugestions made back in March,
none fixed the problem.

I have tried
ilist.append(my_sock.fileno()) - instead of ilist.append(my_sock)
also
il,ol,el = select([my_sock.fileno()],[],[],1) and
il,ol,el = select([my_sock],[],[],1)
instead of il,ol,el = select(ilist,[],[],1)

Do I need to do something to set a default timeout?
Does the default timeout override the select() timeout?

Any help would be appreciated.
Thanks (sorry for the long post),
David Shifflett
 
D

Donn Cave

Hi all,
I am having trouble with a program that ran fine on

Python 2.0 (#0, Mar 1 2001, 01:47:55)
[GCC 2.95.1 19990816 (release)] on linux2

but will not work on

Python 2.3.2 (#1, Oct 8 2003, 17:33:47)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2

These are part of Familiar Linux running on a iPAQ.

The program has a button to cause a packet to be sent to a server,
the program also has a thread to read packets from the server.
I send a packet, I get one back, pretty simple.

I am using select() to wait for packets
and it isn't functioning as expected.

select() doesn't return until I have sent a packet
even though I am using a short timeout (1 second)

You could be running into some kind of bug in select,
but of course statistically the odds are overwhelming
that the bug is in your application.

The code you posted looked OK to me on casual examination,
but it raises some questions I can't answer - for example,
unless I missed something, the "select" function might have
come from "from gtk import *"?

If no one turns up who recognizes this problem, do you
think you could post a complete, minimal program
that exhibits this behavior - no "...", no gtk, etc.?
Might try this with and without threads to see if it
has anything to do with your problem.

Donn Cave, (e-mail address removed)
 
J

Jean Brouwers

Two comments.

1) The lists passed to select() must contain items the fileno() of the
sockets, not the sockets themselves. Same for the returned lists.

2) The last argument to select() is the timeout in seconds, a float.
Instead of 1, try 1.0.

HTH,
/Jean Brouwers


D. said:
Hi all,
I am having trouble with a program that ran fine on

Python 2.0 (#0, Mar 1 2001, 01:47:55)
[GCC 2.95.1 19990816 (release)] on linux2

but will not work on

Python 2.3.2 (#1, Oct 8 2003, 17:33:47)
[GCC 3.3.2 20030908 (Debian prerelease)] on linux2

These are part of Familiar Linux running on a iPAQ.

The program has a button to cause a packet to be sent to a server,
the program also has a thread to read packets from the server.
I send a packet, I get one back, pretty simple.

I am using select() to wait for packets
and it isn't functioning as expected.

select() doesn't return until I have sent a packet
even though I am using a short timeout (1 second)

So whats happening is I send a packet,
select() returns but I have no input yet,
then I have to send a second packet
then select returns and I can read the
response to the first packet.

Here's a snippet of the code

from sys import argv
from gtk import *
import socket
import time
import threading
import select
...
my_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
my_sock.bind(('', MY_PORT))

class readerthread(threading.Thread):
def __init__(self):
self._stopevent = threading.Event()
print "thread init"
threading.Thread.__init__(self, name="readerthread")

def run(self):
ilist = []
ilist.append(my_sock)
print "readerthread - my_sock %d" % my_sock.fileno()
while not self._stopevent.isSet():

print "about to select"
il,ol,el = select(ilist,[],[],1)

# read from the socket, etc
if il != []:
data, addr = my_sock.recvfrom(1024)
print "recv() Data length %d" %len(data)
print "recv() Data %s" % data
else:
print "No input from select"

def join(self,timeout=None):
"""
Stop the thread
"""
self._stopevent.set()
threading.Thread.join(self, timeout)
...
def button_cb(button):
my_sock.sendto(data, (SERVERADDR, SERVERPORT))
...

I first posted this problem back on March 11.
I thought it was due to switching to a Win XP system,
now it seems to be due to switch Python versions.

I have tried all the sugestions made back in March,
none fixed the problem.

I have tried
ilist.append(my_sock.fileno()) - instead of ilist.append(my_sock)
also
il,ol,el = select([my_sock.fileno()],[],[],1) and
il,ol,el = select([my_sock],[],[],1)
instead of il,ol,el = select(ilist,[],[],1)

Do I need to do something to set a default timeout?
Does the default timeout override the select() timeout?

Any help would be appreciated.
Thanks (sorry for the long post),
David Shifflett
 
D

Donn Cave

Jean Brouwers said:
1) The lists passed to select() must contain items the fileno() of the
sockets, not the sockets themselves. Same for the returned lists.
>>> from select import select
>>> from sys import stdin
>>> select([stdin], [], [])

2) The last argument to select() is the timeout in seconds, a float.
Instead of 1, try 1.0.
>>> select([stdin], [], [], 1)
([], [], [])


Donn Cave, (e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top