passing a socket to a spawned process.

Andrew MacIntyre andymac at
Wed Oct 13 13:49:48 CEST 2004

On Wed, 13 Oct 2004, Alex Martelli wrote:

> Marcos Dione <mdione at> wrote:
> > On Mon, Oct 11, 2004 at 10:23:27AM -0700, Mike M wrote:
> > > Is it possible?  In the parent process, I have a socket that binds,
> > > listens and then accepts new connections (which creates new sockets in
> > > the process).  I want to be able to pass some of these new sockets to
> > > a spawned process.  Is it possible, and if so how?
> >
> >   just accept() before fork()'ing. the socket will be cloned. then close the
> > socket on the parent, unless needed.
> Unfortunately I believe that's platform-dependent.  Specifically: your
> suggestion should work on any sensible operating system... BUT a tad
> over 50% of Python programs are estimated to run on Windows, and, on
> THAT platform, I know your idea can't work (no fork!) and I don't know
> how to answer the OP's question (except by suggesting he move, if
> feasible, to any sensible platform -- any BUT Wind0ws...!-).

I've not tried this on Windows, but on OS/2 using the native sockets
(which is close to the Windows socket model as far as I can tell) you have
to pass the socket handle (an integer) to the child (usually via the
command line), which then attaches to the socket, and signals the parent
that it can close its reference to the socket.

The EMX (OS/2, Win32) and Cygwin (Win32) runtime environments synthesize
fork() and socket inheritance over the native environment, though at a
significant cost in run time and resources.

Andrew I MacIntyre                     "These thoughts are mine alone..."
E-mail: andymac at  (pref) | Snail: PO Box 370
        andymac at             (alt) |        Belconnen ACT 2616
Web:               |        Australia

More information about the Python-list mailing list