Viking python, non-existent child

I have some problems with Python child processes, so I wrote a very simple script:

import os import sys import time pid = os.fork() if pid: #parent time.sleep(30) else: #child #os._exit(0) sys.exit() 

While the parent process is sleeping, I run

 ps fax | grep py[t]hon 

And I read this conclusion

 2577 ? S 0:00 python /home/pi/python/GPIO/GPIODaemon.py restart 2583 ? Z 0:00 \_ [python] <defunct> 

Using sys.exit() or os._exit(0) , there is always a Zombie process, and I cannot understand why.

While working on my more complex code, I thought there were some resources that the child processes kept blocked, but there was no file / socket / db connection at all on this simplified root code! Why is the baby process zombie?

+4
source share
3 answers

To clear a child process on Unix, you need to wait for the child, check one of the os.wait (), os.waitpid (), os.wait3 (), or os.wait4 () options at http://docs.python.org /2/library/os.html#os.wait

As for this, this is a Unix design solution. The child process stores the return value in its state of the process, if it disappears, you will not have a return value. The os.wait () function also returns a return value, and then the child process is freed and all associated resources are freed.

+8
source

I had a similar problem: a process started by spawnl , which may end or may need to be terminated at a specific point. My decision to not have all the zombie processes was

 def cleanup_subprocesses(self, pid): try: os.kill(pid, signal.SIGKILL) except OSError: pass os.waitpid(self._pid, 0) 

If the process does not complete with time, it will be killed; in any case, the waitpid command is executed.

This clearly does not help if your program does not have a good point where you know that you no longer need this process.

0
source

I tried using os.kill (), which still leaves a non-existent process. I just close the child (2) so that he does not stick my magazines, and then talk about the failure. It is dirty, but apparently uses python.

0
source

Source: https://habr.com/ru/post/1495615/


All Articles