V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
v1cT0r
V2EX  ›  Python

多线程 queue 问题,求助大神

  •  
  •   v1cT0r · 2019-08-27 16:09:53 +08:00 · 1386 次点击
    这是一个创建于 1720 天前的主题,其中的信息可能已经有所发展或是发生改变。

    运行环境: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()
    
    5 条回复    2019-08-28 15:06:52 +08:00
    v1cT0r
        1
    v1cT0r  
    OP
       2019-08-28 08:40:48 +08:00
    有人能重现这种现象吗?
    1462326016
        2
    1462326016  
       2019-08-28 10:04:43 +08:00
    别的不说,我想问下你开这么多线程干嘛?这都快两千了
    v1cT0r
        3
    v1cT0r  
    OP
       2019-08-28 10:47:25 +08:00
    线程多无非就是增加 cpu 切换带来消耗,但是不能成为死锁的理由。
    1462326016
        4
    1462326016  
       2019-08-28 11:33:37 +08:00
    @v1cT0r 但是我测试无法复现,win10 和 linux ( Ubuntu18.0.4 )均正常
    v1cT0r
        5
    v1cT0r  
    OP
       2019-08-28 15:06:52 +08:00
    专门安装了个 ubuntu18.0.4,果然不会 hang 死,centos7 居然 hang 死,刷新认知了,主要是我们这边生产环境清一色的 rhel 和 centos。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1321 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 23:41 · PVG 07:41 · LAX 16:41 · JFK 19:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.