Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > select() problem, not timing out

Reply
Thread Tools

select() problem, not timing out

 
 
D. Shifflett
Guest
Posts: n/a
 
      07-29-2004
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
 
Reply With Quote
 
 
 
 
Donn Cave
Guest
Posts: n/a
 
      07-29-2004
In article <(E-Mail Removed)> ,
http://www.velocityreviews.com/forums/(E-Mail Removed) (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, (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Jean Brouwers
Guest
Posts: n/a
 
      07-30-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 <(E-Mail Removed)> , D.
Shifflett <(E-Mail Removed)> 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

 
Reply With Quote
 
Donn Cave
Guest
Posts: n/a
 
      07-30-2004
In article <300720041335392859%(E-Mail Removed) pam.net>,
Jean Brouwers <(E-Mail Removed)> 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, (E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ASP.NET SQL Server Session not timing out =?Utf-8?B?VGVqYXN3aQ==?= ASP .Net 1 02-12-2005 12:53 AM
A long running Stored procedure is not timing out. Why?? =?Utf-8?B?SW5kaXJh?= ASP .Net 0 01-10-2005 04:07 AM
Session not timing out with EnableSessionState set to false user_me ASP .Net 1 12-15-2004 05:56 PM
Forms Authentication - Not timing out, not redirecting. AVance ASP .Net Security 3 08-19-2004 02:07 PM
Forms Authentication - Not timing out, not redirecting. AVance ASP .Net 1 07-28-2004 08:23 PM



Advertisments