gdsdhm 2022-02-13 18:00 采纳率: 100%
浏览 148
已结题

python多进程中使用Queue后,无法退出进程,出现僵尸进程,如下,求解

问题遇到的现象和发生背景

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和内存等资源,而不是出现僵尸进程。

  • 写回答

2条回答 默认 最新

  • 愿此后再无WA Python领域新星创作者 2022-02-13 21:41
    关注

    题主您好,由于自身能力有限,无法为您解答。
    为您找到以下文章:
    https://blog.csdn.net/Freshduke/article/details/111544319

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 创建了问题 2月13日

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图