Select on Win XP

D. Shifflett shifflett at nps.navy.mil
Thu Mar 11 17:44:26 EST 2004


Hi all,
I am trying to run a Python program
on my Win XP box, that I brought over from a Linux box.
Of course, on Linux it works fine, but not so on Win XP.

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 the packet again
then select returns and I can read the 
response to the first packet.

Hoepfully I am doing something wrong on the Win system.
Any help would be appreciated.

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))
...



More information about the Python-list mailing list