Sharing file descriptors across processes

Donn Cave donn at u.washington.edu
Wed Sep 5 14:01:09 EDT 2001


Quoth <dave at pythonapocrypha.com>:
| I need to somehow "pass" a file descriptor (socket) from one process to
| another process, does anybody know how this can be done? More specifically
| I have a program that accepts an incoming socket and then opens another
| socket to a server, and then acts as a proxy, passing data between the
| two. I would like to find some way to take that pair of sockets and hand
| them off to another worker process. Here's some additional info:

On platforms that have AT&T STREAMS, you might look at man 7 streamio
and read up on I_SENDFD.  I don't see anything that says the descriptor
has to be a Stream itself, only the pipe over which it's sent.  Have
never tried it.  I wouldn't say for sure there isn't some semi-standard
socket protocol for this, outside of the STREAMS package, but right now
it eludes me if there is one.

| - This is nearly identical to a main process forking off work to a child
| worker process, except that forking each time is too expensive. I can fork
| off the children initially (if that will help somehow, e.g. if that lets
| the children share the file descriptor tables or something), but the
| parent and the children need to be long-running, where the parent is
| continually passing off work to the kids.

That can work, if the worker threads can create the sockets.  Let them
do the accept, under control from the parent as required.  The parent
can use select() to tell when there's something to accept.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list