sockets - recv with time out

Joseph A Knapka jknapka at
Fri Jan 11 14:33:03 EST 2002

Steve Holden wrote:
> "David Bolen" <db3l at> wrote in message
> news:uy9j5a0ds.fsf at
> > westernsam at (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.

Er... how's that? Given the OP's stated requirements,
what's wrong with:

timeout = 10 # seconds
readable = select([sock],None,None,timeout)
if readable:
    data2 = sock.recv()


This code won't make you wait any longer than
timeoutsocket would, no matter what the client


-- Joe
"I should like to close this book by sticking out any part of my neck
 which is not yet exposed, and making a few predictions about how the
 problem of quantum gravity will in the end be solved."
 --- Physicist Lee Smolin, "Three Roads to Quantum Gravity"

More information about the Python-list mailing list