Sockets accept() in child processes

Dan Stromberg drsalists at gmail.com
Thu Apr 12 15:48:31 EDT 2012


On Thu, Apr 12, 2012 at 10:48 AM, Merwin <merwin.irc at gmail.com> wrote:

> Le 12/04/2012 19:10, Dan Stromberg a écrit :
>
>
>> I wonder if this'll do what you need:
>> https://trac.calendarserver.**org/browser/CalendarServer/**
>> trunk/twext/python/sendfd.py<https://trac.calendarserver.org/browser/CalendarServer/trunk/twext/python/sendfd.py>
>>
>
> The problem is that this is Linux-only solution, and I would like to keep
> a multi-platform compatibility.
>
I suspect it's actually pretty multiplatform, with one likely exception:
Windows.  But the code itself is from Apple, and I first learned of the
technique in Unix's heyday.


> There are other ways to do this : use a Lock and do the select() directly
> in the children processes,
> but I can't control which process will get the lock, and it's totally
> possible that only 2 processes on 8
> will accept connections alternatively.
>
If you have all your children bind using SO_REUSEADDR, and then have the
parent process pass a "go ahead message" to an available child, you might
get what you desire.  The unavailable child would then pass a "I'm
available again" message back to the parent when ready for another piece of
work.

The children probably should also be able to send a "I'm done with the
socket" message to the parent as well, so the parent doesn't ask two
children to use the socket at the same time.

This is kind of weird, but it seems to satisfy your requirement.

I believe SO_REUSEADDR is even more cross-platform than passing a
filedescriptor.

Better still, don't have the children interact with the main socket; just
let them get a complete message describing a piece of work from the
parent.  This isn't as likely to run into OS idiosyncrasies.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120412/79b9dd3b/attachment-0001.html>


More information about the Python-list mailing list