多线程 queue 问题,求助大神

2019-08-27 16:09:53 +08:00
 v1cT0r

运行环境:CentOS Linux release 7.6.1810 + Python 3.6.8

以下代码在 linux 跑总是会死锁 hang 死, windows 10 下运行正常,这个怎么理解?

# coding: utf-8

import queue
import time
import _thread

class QueueTest(object):

    def __init__(self):

        self.q = queue.Queue()

    def test_function(self,num):

        while True:
            self.q.put("this is number: %s" % num)
            time.sleep(0.1)

    def producer(self):
        for i in range(1500):
            _thread.start_new_thread(self.test_function,(i,))

    def consumer(self):
        while True:
            num = self.q.get()
            print(f"get message: {num}\n")

    def print_qsize(self):
        while True:
            self.qsize = self.q.qsize()
            print(f"this queue size is {self.qsize}\n")
            time.sleep(1)

test = QueueTest()

test.producer()
for i in range(150):
    _thread.start_new_thread(test.consumer,())
test.print_qsize()
1393 次点击
所在节点    Python
5 条回复
v1cT0r
2019-08-28 08:40:48 +08:00
有人能重现这种现象吗?
1462326016
2019-08-28 10:04:43 +08:00
别的不说,我想问下你开这么多线程干嘛?这都快两千了
v1cT0r
2019-08-28 10:47:25 +08:00
线程多无非就是增加 cpu 切换带来消耗,但是不能成为死锁的理由。
1462326016
2019-08-28 11:33:37 +08:00
@v1cT0r 但是我测试无法复现,win10 和 linux ( Ubuntu18.0.4 )均正常
v1cT0r
2019-08-28 15:06:52 +08:00
专门安装了个 ubuntu18.0.4,果然不会 hang 死,centos7 居然 hang 死,刷新认知了,主要是我们这边生产环境清一色的 rhel 和 centos。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/595572

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX