os.fork leaving processes behind

F

Falcolas

Hi all,

This may be more of a Linux question, but it relates to how Python
forks...

Today, I implemented a pretty simple listener script using os.fork.
The script runs fine, and performs as expected, but the process table
is left with an odd entry for every fork called.

I'm running on Slackware 9, under the 2.4 kernel, Python 2.5.1.

while True:
conn, addr = s.accept()
if os.fork():
continue
else:
handle_connection(conn)
sys.exit(0)

Running ps -ef results in a slew of '[ python <depreciated> ]' entries
(or something similar, I no longer have the actual output).

Will these clean themselves up if I leave the process running, and
what causes these?

Thanks in advance, G
 
G

Gabriel Genellina

This may be more of a Linux question, but it relates to how Python
forks...

Yes; every book describing how to use fork tells about zombie processes
and the wait/waitpid functions. Just do the same thing in Python.
Today, I implemented a pretty simple listener script using os.fork.
The script runs fine, and performs as expected, but the process table
is left with an odd entry for every fork called.

A "zombie".
I'm running on Slackware 9, under the 2.4 kernel, Python 2.5.1.

while True:
conn, addr = s.accept()
if os.fork():
continue
else:
handle_connection(conn)
sys.exit(0)

I'd try to use any of the existing server implementations in
SocketServer.py, but if you insist on using your own, look at the
ForkingMixin class as an example of using waitpid() to avoid having zombie
processes.
 
F

Falcolas

I'd try to use any of the existing server implementations in
SocketServer.py, but if you insist on using your own, look at the
ForkingMixin class as an example of using waitpid() to avoid having zombie
processes.

Thanks for the excellent advice. I went with the SocketServer, and I'm
quite happy with the results. I had not considered it earlier due to
the atrocious documentation on the Python site. Google helped with
that immensely.

G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,807
Members
48,797
Latest member
PeterSimpson
Top