python僵尸進(jìn)程產(chǎn)生的原因
在 unix 或 unix-like 的系統(tǒng)中,當(dāng)一個子進(jìn)程退出后,它就會變成一個僵尸進(jìn)程,如果父進(jìn)程沒有通過 wait 系統(tǒng)調(diào)用來讀取這個子進(jìn)程的退出狀態(tài)的話,這個子進(jìn)程就會一直維持僵尸進(jìn)程狀態(tài)。
Zombie process - Wikipedia 中是這樣描述的:
On Unix and Unix-like computer operating systems, a zombie process or defunct process is a process that has completed execution (via the exit system call) but still has an entry in the process table: it is a process in the "Terminated state". This occurs for child processes, where the entry is still needed to allow the parent process to read its child's exit status: once the exit status is read via the wait system call, the zombie's entry is removed from the process table and it is said to be "reaped". A child process always first becomes a zombie before being removed from the resource table. In most cases, under normal system operation zombies are immediately waited on by their parent and then reaped by the system – processes that stay zombies for a long time are generally an error and cause a resource leak.
并且僵尸進(jìn)程無法通過 kill 命令來清除。
本文將探討如何手動制造一個僵尸進(jìn)程以及清除僵尸進(jìn)程的辦法。
手動制造一個僵尸進(jìn)程
為了便于后面講解清除僵尸進(jìn)程的方法,我們使用日常開發(fā)中經(jīng)常使用的 multiprocessing 模塊來制造僵尸進(jìn)程(準(zhǔn)確的來說是制造一個長時間維持僵尸進(jìn)程狀態(tài)的子進(jìn)程):
$ cat test_a.py from multiprocessing import Process, current_process import logging import os import time logging.basicConfig( level=logging.DEBUG, format='%(asctime)-15s - %(levelname)s - %(message)s' ) def run(): logging.info('exit child process %s', current_process().pid) os._exit(3) p = Process(target=run) p.start() time.sleep(100)
測試:
$ python test_a.py & [1] 10091 $ 2017-07-20 21:28:14,792 - INFO - exit child process 10106 $ ps aux |grep 10106 mozillazg 10126 0.0 0.0 2434836 740 s006 R+ 0:00.00 grep 10106 mozillazg 10106 0.0 0.0 0 0 s006 Z 0:00.00 (Python)
可以看到,子進(jìn)程 10091 變成了僵尸進(jìn)程。
既然已經(jīng)可以控制僵尸進(jìn)程的產(chǎn)生了,那我們就可以進(jìn)入下一步如何清除僵尸進(jìn)程了。
清除僵尸進(jìn)程有兩種方法:
•第一種方法就是結(jié)束父進(jìn)程。當(dāng)父進(jìn)程退出的時候僵尸進(jìn)程隨后也會被清除。
• 第二種方法就是通過 wait 調(diào)用來讀取子進(jìn)程退出狀態(tài)。我們可以通過處理 SIGCHLD 信號,在處理程序中調(diào)用 wait 系統(tǒng)調(diào)用來清除僵尸進(jìn)程。
處理 SIGCHLD 信號
子進(jìn)程退出時系統(tǒng)會向父進(jìn)程發(fā)送 SIGCHLD 信號,父進(jìn)程可以通過注冊 SIGCHLD 信號處理程序,在信號處理程序中調(diào)用 wait
系統(tǒng)調(diào)用來清理僵尸進(jìn)程。 $ cat test_b.py
import errno from multiprocessing import Process, current_process import logging import os import signal import time logging.basicConfig( level=logging.DEBUG, format='%(asctime)-15s - %(levelname)s - %(message)s' ) def run(): exitcode = 3 logging.info('exit child process %s with exitcode %s', current_process().pid, exitcode) os._exit(exitcode) def wait_child(signum, frame): logging.info('receive SIGCHLD') try: while True: # -1 表示任意子進(jìn)程 # os.WNOHANG 表示如果沒有可用的需要 wait 退出狀態(tài)的子進(jìn)程,立即返回不阻塞 cpid, status = os.waitpid(-1, os.WNOHANG) if cpid == 0: logging.info('no child process was immediately available') break exitcode = status >> 8 logging.info('child process %s exit with exitcode %s', cpid, exitcode) except OSError as e: if e.errno == errno.ECHILD: logging.error('current process has no existing unwaited-for child processes.') else: raise logging.info('handle SIGCHLD end') signal.signal(signal.SIGCHLD, wait_child) p = Process(target=run) p.start() while True: time.sleep(100)
效果:
$ python test_b.py & [1] 10159 $ 2017-07-20 21:28:56,085 - INFO - exit child process 10174 with exitcode 3 2017-07-20 21:28:56,088 - INFO - receive SIGCHLD 2017-07-20 21:28:56,089 - INFO - child process 10174 exit with exitcode 3 2017-07-20 21:28:56,090 - ERROR - current process has no existing unwaited-for child processes. 2017-07-20 21:28:56,090 - INFO - handle SIGCHLD end $ ps aux |grep 10174 mozillazg 10194 0.0 0.0 2432788 556 s006 R+ 0:00.00 grep 10174
可以看到,子進(jìn)程退出變成僵尸進(jìn)程后,系統(tǒng)給父進(jìn)程發(fā)送了 SIGCHLD 信號,我們在 SIGCHLD 信號的處理程序中通過 os.waitpid 調(diào)用 wait 系統(tǒng)調(diào)用后阻止了子進(jìn)程一直處于僵尸進(jìn)程狀態(tài),從而實現(xiàn)了清除僵尸進(jìn)程的效果。
相關(guān)文章
Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值詳解
在實際處理數(shù)據(jù)中,數(shù)據(jù)預(yù)處理操作中,常常需要去除掉重復(fù)的數(shù)據(jù),這篇文章主要給大家介紹了關(guān)于Python?Pandas中DataFrame.drop_duplicates()刪除重復(fù)值的相關(guān)資料,需要的朋友可以參考下2022-07-07Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實例
這篇文章主要介紹了Django ORM判斷查詢結(jié)果是否為空,判斷django中的orm為空實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07Python中線程threading.Thread的使用詳解
python的thread模塊是比較底層的模塊,python的threading模塊是對thread做了一些包裝的,可以更加方便的被使用。本文將為大家詳細(xì)介紹一下python中的線程threading.Thread()的使用,需要的可以參考一下2022-07-07tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法
今天小編就為大家分享一篇tensorflow 恢復(fù)指定層與不同層指定不同學(xué)習(xí)率的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法
這篇文章主要介紹了Python使用xlrd模塊操作Excel數(shù)據(jù)導(dǎo)入的方法,涉及Python操作xlrd模塊的技巧,需要的朋友可以參考下2015-05-05Python supervisor強(qiáng)大的進(jìn)程管理工具的使用
這篇文章主要介紹了Python supervisor強(qiáng)大的進(jìn)程管理工具的使用,本文主要跟大家分享在類unix操作系統(tǒng)下supervisor的使用以及一些關(guān)于進(jìn)程的知識,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-04-04python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5布局控件QGridLayout詳細(xì)使用方法與實例,需要的朋友可以參考下2020-03-03