程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

After using queue in Python multiprocesses, the process cannot be exited, and a zombie process appears. The following is the solution

編輯:Python
The phenomenon and background of the problem

linux Environmental Science ,python3 Use in multiple processes Queue after , Unable to exit process , Zombie process , as follows , solve

import timefrom multiprocessing import Process, Queueclass Test1(Process): def __init__(self, q, ip): super().__init__() self.q = q self.ip = ip def run(self): index = 0 while True: time_str = str(int(time.time()*1000)) self.q.put(time_str) print(self.ip, index) time.sleep(2) # Turn off the signal todo: But cannot exit normally , Become a dead process  if index >=5 and self.ip == '192.168.1.64': print(f'*********ending process*********:{self.ip}') self.q.cancel_join_thread() self.q.close() break if index >=10 and self.ip == '192.168.1.65': print(f'*********ending process*********:{self.ip}') self.q.cancel_join_thread() self.q.close() break if index >=15 and self.ip == '192.168.1.66': print(f'*********ending process*********:{self.ip}') self.q.cancel_join_thread() self.q.close() break index += 1 print(f'*********test end*********: {self.ip}')class Test2(Process): def __init__(self, q, ip): super().__init__() self.q = q self.ip = ip def run(self): index = 0 while True: # self.q.get() index += 1def run(): ips = [ "192.168.1.64", "192.168.1.65", "192.168.1.66" ] queues = [Queue() for _ in ips] processes = [] for queue, camera_ip in zip(queues, ips): # production  test = Test1(queue, camera_ip) processes.append(test) # consumption  test2 = Test2(queue, camera_ip) processes.append(test2) for process in processes: process.start() for process in processes: process.join()if __name__ == '__main__': run()
Operation results and error reporting contents

ps see python process ,23055/23057 The production process did not really exit

ps -aux | grep python

root 23052 0.0 0.0 30632 13720 pts/1 S+ 13:10 0:00 python test_queue_close.py
root 23054 99.9 0.0 30632 8908 pts/1 R+ 13:10 270:11 python test_queue_close.py
root 23055 0.0 0.0 0 0 pts/1 Z+ 13:10 0:00 [python]
root 23056 99.9 0.0 30632 8908 pts/1 R+ 13:10 270:10 python test_queue_close.py
root 23057 0.0 0.0 0 0 pts/1 Z+ 13:10 0:00 [python]
root 23059 99.9 0.0 30632 8908 pts/1 R+ 13:10 270:08 python test_queue_close.py
sean 24805 0.0 0.0 21544 1160 pts/1 S+ 17:41 0:00 grep --color=auto python

My solution ideas and tried methods

Tried :
1. Comment off Test2 After consumption process , You can exit normally ;
2. Use only one ip A queue 、 A production process 、 A consumption process , There will be no mistakes . Can exit normally ;

What I want to achieve

Turn off the signal ( Any one ip Production process ), Can exit the process normally , Release cpu And memory , Instead of a zombie process .




Take the answer :

Hello, questioner , Due to their limited ability , I can't answer for you .
Find the following articles for you :
https://blog.csdn.net/Freshduke/article/details/111544319



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved