问题遇到的现象和发生背景
linux环境,python3多进程中使用Queue后,无法退出进程,出现僵尸进程,如下,求解
问题相关代码,请勿粘贴截图
import time
from multiprocessing import Process, Queue
class 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)
# 关闭信号 todo:但无法正常退出,成为死进程
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 += 1
def 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):
#生产
test = Test1(queue, camera_ip)
processes.append(test)
#消费
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查看python进程,23055/23057 生产进程并没能真正退出
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
我的解答思路和尝试过的方法
尝试过:
1.注释关闭Test2 消费进程后,可以正常退出;
2.只使用一个ip即一个队列、一个生产进程、一个消费进程,则不会出现错误。能正常退出;
我想要达到的结果
关闭信号(任意一个ip生产进程),能正常退出进程,释放cpu和内存等资源,而不是出现僵尸进程。