request of "real timeout" in socket.read

顺航 游 ysh1989 at gmail.com
Mon Nov 21 02:32:29 EST 2011


Hi, few days ago I was confused with the behavior of socket.read when
timeout is used -- that the lifespan of the operation often is longer
than the time I've specified. So I go to python.org and get the source
code of python.
Yet I had read some lines of code from the Python-2.7.2/Modules/
socketmodule.c, and I found that the function was implemented in
internal_select by poll or select, this function will block at most a
span of timeout, so whenever it is called, another timeout time has to
be wasted if there's nothing coming.
So what if to change the code like this:
now = time(NULL); //may be other function
if( s->sock_timeout >= now + .5 )
    return 1;

timeout = s->sock_timeout + .5 - now;
/*
... poll or select
*/

here s->sock_timeout is a particular moment rather than a time span,
or:
s->sock_timeout = time(NULL) + timeout;
when init.



More information about the Python-list mailing list