Jeremy
Jeremy

Reputation: 1143

Stop python sub-process leaving behind dead 'python' process

I am working on a python program which has to run a (possibly large) number of other python programs (for some load testing). Some of these are short-lived and others keep going for longer. I want the short-lived ones to terminate and disappear when they are done, but each one seems to leave behind a dead "(Python)" process until the original program finishes.

Platform is OSX, Python 2.7.6.

Here are simple demo programs that show the issue:

#!/usr/bin/env python
# Child Process: Started by parent...
import time
print "Hello from child!"
time.sleep(10)
print "Child finished."
exit(0)

And the parent program:

#!/usr/bin/env python
# Parent process:
import subprocess, time
print "Starting Child..."
child = subprocess.Popen( "./child.py" )
print "Parent Process... hanging around."
time.sleep(30)
exit(0)

I run the "parent" program from the terminal with "./parent.py". Here's the output (as expected):

Starting Child...
Parent Process... hanging around.
Hello from child!
Child finished.

Initially the 'ps' command shows this:

'ps' command shows this:

782 ttys002    0:00.03 python ./parent.py
783 ttys002    0:00.02 python ./child.py

This makes sense... the parent has launched the child process.

After 10 seconds, the child finishes as expected, and 'ps' then shows:

782 ttys002    0:00.03 python ./parent.py
783 ttys002    0:00.00 (Python)

'783' then hangs around until the parent process finishes, even though the python program has already done "exit(0)".

Is there any way to actually terminate / get rid of this process once it's done? I'm thinking it might be an issue if I launch hundreds or even thousands of short-lived processes over a long time.

I've experimented with subprocess(), Popen (as shown), and also using fork() and then execl(), but they always end up with the same behaviour.

Upvotes: 0

Views: 568

Answers (1)

bav
bav

Reputation: 1623

You should call child.wait at some point.

Upvotes: 3

Related Questions