Extend socket.accept to put accepted socket in non-blocking mode
Hi, (I hope this list is the right place for this) I was browsing around for accept4 usage in Python and I saw it's already used internally in socket.accept (on Linux only), albeit it's not exposed, so accepting an incoming connection and setting it to be non-blocking takes 3 syscalls. accept4 allows us to do this with a single syscall, and it made it into FreeBSD 10, so that's another system that could benefit from this optimization. Would it be desirable to extend socket.accept to something like: socket.accept(set_non_blocking=False) which would use accept4's flags on supported systems and fallback to regular fcntl on others? The idea is to accept an incoming connection and make it non-blocking at the same time, asyncio and other frameworks would benefit from this by doing one function call and 2 syscalls less (on supported systems, that is). Cheers, -- Saúl Ibarra Corretgé bettercallsaghul.com
On Fri, 07 Mar 2014 10:37:58 +0100
Saúl Ibarra Corretgé
accept4 allows us to do this with a single syscall, and it made it into FreeBSD 10, so that's another system that could benefit from this optimization.
If it doesn't, then perhaps the configure script needs to be fixed.
The idea is to accept an incoming connection and make it non-blocking at the same time, asyncio and other frameworks would benefit from this by doing one function call and 2 syscalls less (on supported systems, that is).
That's not likely to do a significant difference (benchmarks welcome). Regards Antoine.
On 03/07/2014 11:40 AM, Antoine Pitrou wrote:
On Fri, 07 Mar 2014 10:37:58 +0100 Saúl Ibarra Corretgé
wrote: accept4 allows us to do this with a single syscall, and it made it into FreeBSD 10, so that's another system that could benefit from this optimization.
If it doesn't, then perhaps the configure script needs to be fixed.
Probably not, I'll have a look.
The idea is to accept an incoming connection and make it non-blocking at the same time, asyncio and other frameworks would benefit from this by doing one function call and 2 syscalls less (on supported systems, that is).
That's not likely to do a significant difference (benchmarks welcome).
Actually, after http://hg.python.org/cpython/rev/5f0d1aad7322/ it's 2 function calls (accept + set_blocking)+ 2 syscalls (accept + ioctl FIONBIO) vs 1 function call (accept) + 1 syscall (accept4). -- Saúl Ibarra Corretgé bettercallsaghul.com
accept4() was added for atomicity (O_CLOEXEC), not performance.
For pipe2() vs pipe(), this could make a measurable difference because the
base syscall is so fast, but for accept(), which is a blocking syscall,
shaving one or two syscalls are unlikely to yield any performance gain.
IMO that's not a reason good enough to make accept() signature more complex.
cf
2014-03-07 11:14 GMT+00:00 Saúl Ibarra Corretgé
On 03/07/2014 11:40 AM, Antoine Pitrou wrote:
On Fri, 07 Mar 2014 10:37:58 +0100 Saúl Ibarra Corretgé
wrote: accept4 allows us to do this with a single syscall, and it made it into FreeBSD 10, so that's another system that could benefit from this optimization.
If it doesn't, then perhaps the configure script needs to be fixed.
Probably not, I'll have a look.
The idea is to accept an incoming connection and make it non-blocking at
the same time, asyncio and other frameworks would benefit from this by doing one function call and 2 syscalls less (on supported systems, that is).
That's not likely to do a significant difference (benchmarks welcome).
Actually, after http://hg.python.org/cpython/rev/5f0d1aad7322/ it's 2 function calls (accept + set_blocking)+ 2 syscalls (accept + ioctl FIONBIO) vs 1 function call (accept) + 1 syscall (accept4).
-- Saúl Ibarra Corretgé bettercallsaghul.com _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
On 03/07/2014 11:40 AM, Antoine Pitrou wrote:
On Fri, 07 Mar 2014 10:37:58 +0100 Saúl Ibarra Corretgé
wrote: accept4 allows us to do this with a single syscall, and it made it into FreeBSD 10, so that's another system that could benefit from this optimization.
If it doesn't, then perhaps the configure script needs to be fixed.
I checked and the condigure script seems to do the right thing, so FreeBSD 10 should just use accpet4 without nay change. -- Saúl Ibarra Corretgé bettercallsaghul.com
On Fri, Mar 7, 2014 at 8:37 PM, Saúl Ibarra Corretgé
The idea is to accept an incoming connection and make it non-blocking at the same time, asyncio and other frameworks would benefit from this by doing one function call and 2 syscalls less (on supported systems, that is).
Is the benefit that it's simply more efficient, or is there a specific benefit from atomicity? The latter is a pretty strong argument (look at close-on-exec for instance), if it applies. ChrisA
participants (4)
-
Antoine Pitrou
-
Charles-François Natali
-
Chris Angelico
-
Saúl Ibarra Corretgé