Passing file descriptors in Python?

Dan puter_programmer at yahoo.com
Sat Oct 2 02:54:23 EDT 1999


I suppose I should clarify my request. :-)

What I want to do is to have a parent process spawn off a child
process which then waits for new file descriptor (socket) to come from
the parent.  The parent after it spawns several children will wait
listen on a socket for new connections which it will pass to one of
the child processes after the connection has been established.

My goal here is to make a Python based web server that will run on
more than one processor simultaniously.  I've already built one that
uses threading but unfortunately Python has a global interpreter lock
which keeps two threads from running at the same time on a
multi-processor machine.  So I need to go with a multi-process scheme
in order to take advantage of a multi-processor system but the problem
is that I'm having a difficult time figuring out how to pass a file
descriptor from one process ro another since the only sample code I
can find is written in C.

-Dan



On Fri, 1 Oct 1999 20:30:34 -0600, nascheme at enme.ucalgary.ca wrote:

>On Sat, Oct 02, 1999 at 02:12:28AM +0000, Dan wrote:
>> Has anyone attempted to pass file descriptors from one process to
>> another under using Python on Linux?  I want to build a little web
>> server that accepts connections and hands them off to child processes
>> exactly like Apache does.
>
>I don't know too much about how apache works but fork works just
>fine with Python:
>
>>>> import os
>>>> f = open('foo', 'w')
>>>> if os.fork() == 0:
>...     print 'child', f.fileno()
>... else:
>...     print 'parent', f.fileno()
>... 
>parent 3
>child 3
>
>





More information about the Python-list mailing list