select() problem, not timing out

Discussion in 'Python' started by D. Shifflett, Jul 29, 2004.

  1. D. Shifflett

    D. Shifflett Guest

    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. Shifflett, Jul 29, 2004
    #1
    1. Advertising

  2. D. Shifflett

    Donn Cave Guest

    In article <>,
    (D. Shifflett) 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)


    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,
    Donn Cave, Jul 29, 2004
    #2
    1. Advertising

  3. 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 <>, D.
    Shifflett <> 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
    Jean Brouwers, Jul 30, 2004
    #3
  4. D. Shifflett

    Donn Cave Guest

    In article <300720041335392859%>,
    Jean Brouwers <> wrote:

    > 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], [], [])


    ([<open file '<stdin>', mode 'r' at 0x110ef0>], [], [])


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


    >>> select([stdin], [], [], 1)

    ([], [], [])


    Donn Cave,
    Donn Cave, Jul 31, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AVance
    Replies:
    1
    Views:
    3,102
    AVance
    Jul 28, 2004
  2. user_me
    Replies:
    1
    Views:
    566
    Saravana
    Dec 15, 2004
  3. =?Utf-8?B?SW5kaXJh?=
    Replies:
    0
    Views:
    556
    =?Utf-8?B?SW5kaXJh?=
    Jan 10, 2005
  4. =?Utf-8?B?VGVqYXN3aQ==?=

    ASP.NET SQL Server Session not timing out

    =?Utf-8?B?VGVqYXN3aQ==?=, Feb 11, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    613
    =?Utf-8?B?VGVqYXN3aQ==?=
    Feb 12, 2005
  5. AVance
    Replies:
    3
    Views:
    142
    AVance
    Aug 19, 2004
Loading...

Share This Page