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()
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
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 ;
Turn off the signal ( Any one ip Production process ), Can exit the process normally , Release cpu And memory , Instead of a zombie process .
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