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?
kill
only sends a "signal" to the process and asks it to terminate it work. If you howeverkill
it with-9
it is killed by the operating system. But this is considered a bad way to end a process. – Gesualdokill -9
does the same thing as thesubprocess.kill()
function. Whether I try to terminate it cleanly withp.terminate()
or kill it withp.kill()
it still ends up as a zombie process. – NiplePopen.(['ffmpeg' etc...])
to save UDP data from localhost into anmp3
file. When I end execution through the IDE everything works fine, but when I try to end it through the code itself withPopen.terminate()
andPopen.wait()
the file size iszero
. – Ipa