socket.rcv timeout while-loop

Dave Angel davea at ieee.org
Fri Feb 4 07:10:18 CET 2011


On 01/-10/-28163 02:59 PM, Dwayne Blind wrote:
> or rather
>
>       timeout = s.gettimeout()
>       b=time.clock()
>       while time.clock()-b<3 :
>            s.settimeout(3-time.clock()+b)
>             try :
>                 data=s.recv(1024)
>            except :
>                 break
>       s.settimeout(timeout)
>
> Sorry for all these messages
>
> Dwayne
>

You accidentally top-posted, so I had to delete all the history.

Without knowing anything about "s", there are two problems with this logic:

1) if you loop through the while more than once, you'll be throwing out 
old data.  So you might need something like  data += s.recv(1024). 
Conversely, if an exception happens, data is completely undefined. 
Without defining a default value, your remaining code is likely to get 
an exception.

2) Your time spent might vary between 3 and 6 seconds.  If you need a 
tighter control than that, play with the timeout a little.  For example, 
you might want a 1/2 second timeout, and loop until the total is 3 
seconds.  That way the tolerance will be 3 to 3.5 seconds.

Bonus:  I don't know the behavior of the object, so I don't know what 
state it's in after you timeout.

DaveA

-- 
--
davea at ieee.org



More information about the Python-list mailing list