[Python-Dev] Modules/socketmodule.c: avoiding second fcntl() call worth the effort?

Peter Portante peter.a.portante at gmail.com
Sun Jan 20 05:49:04 CET 2013


Hello folks,

I noticed while stracing a process that sock.setblocking() calls always
result in pairs of fcntl() calls on Linux. Checking 2.6.8, 2.7.3, and 3.3.0
Modules/socketmodule.c, the code seems to use the following (unless I have
missed something):

    delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
    if (block)
        delay_flag &= (~O_NONBLOCK);
    else
        delay_flag |= O_NONBLOCK;
    fcntl(s->sock_fd, F_SETFL, delay_flag);

Perhaps a check to see the flags changed might be worth making?

    int orig_delay_flag = fcntl(s->sock_fd, F_GETFL, 0);
    if (block)
        delay_flag = orig_delay_flag & (~O_NONBLOCK);
    else
        delay_flag = orig_delay_flag | O_NONBLOCK;
    if (delay_flag != orig_delay_flag)
        fcntl(s->sock_fd, F_SETFL, delay_flag);

OpenStack Swift using the Eventlet module, which sets the accepted socket
non-blocking, resulting in twice the number of fcntl() calls. Not a killer
on performance, but it seems simple enough to save a system call here.

Thanks for your consideration,

-peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20130119/1064a6ca/attachment.html>


More information about the Python-Dev mailing list