sockets - recv with time out

Robin Munn rmunn at pobox.com
Fri Jan 11 13:23:55 EST 2002


On Fri, 11 Jan 2002 09:14:50 -0500, Steve Holden <sholden at holdenweb.com> wrote:
>"David Bolen" <db3l at fitlinxx.com> wrote in message
>news:uy9j5a0ds.fsf at ctwd0143.fitlinxx.com...
>> westernsam at hotmail.com (sam) writes:
>>
>> >                            I know generally this should be handled
>> > better, but seeing as am working with what I'm given is there a way I
>> > can do a recv with a timeout (to throw an exception, when the timeout
>> > is reached)? Is this something that can be achieved with non-blocking
>> > sockets and if so how?
>>
>> Typically you'd select() on the socket before doing the recv() - the
>> select() can have a timeout.  Shouldn't be any particular need for
>> non-blocking sockets.
>>
>Erm, how would you do a recv() when your program is blocked waiting for
>select() to return? Non-blocking sockets are an absolute necessity for this
>application, and timeoutsocket is the best solution, as Oleg has already
>said.

Huh? I don't understand what the problem is. select() will return
immediately when one of the following conditions occurs:

1) One of the sockets on the read list has data available.

2) The timeout occurs.

Which of these two has happened is easily determined from the return
value of select(). Now if the timeout is reached, you can close the
socket. If, on the other hand, there's data available to read on one of
the sockets, then a recv() on that socket won't block.

So no, you can't do a recv() when your program is blocked waiting for
select() to return. But then, in that case, the recv() would block,
anyhow. select() will return immediately once data is available to
recv(), and it also has a timeout. As David said, I also don't see any
need for non-blocking sockets here.

-- 
Robin Munn
rmunn at pobox.com



More information about the Python-list mailing list