We are using a python process to manage long running python subprocesses. Subprocesses occasionally need to be killed. The kill command does not completely kill the process, only makes it defunct.
Running the following script demonstrates this behaviour.
import subprocess
p = subprocess.Popen(['sleep', '400'], stdout=subprocess.PIPE, shell=False)
or
p = subprocess.Popen('sleep 400', stdout=subprocess.PIPE, shell=True)
Will create a subprocess.
p.terminate()
p.kill()
does nothing to the process. Demonstrated by ps aux | grep sleep
$ ps aux| grep 'sleep'
User 8062 0.0 0.0 7292 764 pts/7 S 14:53 0:00 sleep 400
The process has not been killed/made defunct. Using the subprocess.call()
function with 'kill'
and pid
as arguments will issue the kill command.
subprocess.call(['kill', str(p.pid)])
This will kill the process but it is now defunct.
$ ps aux | grep 'sleep'
User 8062 0.0 0.0 0 0 pts/7 Z+ 14:51 0:00 [sleep] <defunct>
If the queue is running long enough will it eventually reach its maximum number of processes, or will it eventually reap the defunct processes and be fine?
If the answer is the former, how can I handle defunct processes in python without killing the parent process?
Is there a better way of killing processes?