select() problem, not timing out

Jean Brouwers JBrouwersAtProphICyDotCom at no.spam.net
Fri Jul 30 16:36:20 EDT 2004


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


In article <da4423a8.0407291047.5dbc12e at posting.google.com>, D.
Shifflett <shifflett at nps.navy.mil> wrote:

> 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



More information about the Python-list mailing list