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
cc7756789
V2EX  ›  Python

multiprocessing.Queue() 和 Queue.Queue() 有区别吗?

  •  
  •   cc7756789 · 2015-05-18 10:47:19 +08:00 · 3544 次点击
    这是一个创建于 3278 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用法都一样, 但下面这段代码,Queue从 Queue.Queue导入,read函数便没有执行

    from multiprocessing import Pool, Process, Queue
    import os, time, random

    def write(q):
    for v in ['A', 'B', 'C']:
    print 'Put %s to queue ' % v
    q.put_nowait(v)
    time.sleep(random.random())

    def read(q):
    while 1:
    if not q.empty():
    v = q.get(True)
    print "Get %s from queue" % v
    time.sleep(random.random())
    else:
    break

    if __name__ == '__main__':
    q = Queue()
    pw = Process(target=write, args=(q, ))
    pr = Process(target=read, args=(q, ))
    pw.start()
    pw.join()

    pr.start()
    pr.join()

    print "all done.."
    4 条回复    2015-06-16 17:56:47 +08:00
    clino
        1
    clino  
       2015-05-18 11:48:14 +08:00
    multiprocessing.Queue 这个重点是支持进程间的通讯
    第二个应该木有这种功能吧
    cc7756789
        2
    cc7756789  
    OP
       2015-05-18 15:55:32 +08:00
    @clino 我的理解就是Queue模块是给多线程用的,多线程之间可以共享内存,而multiprocessing自带的Queue支持多进程间的通讯 ?
    clino
        3
    clino  
       2015-05-18 16:18:00 +08:00
    @cc7756789
    文档不看的?
    '''
    multiprocessing supports two types of communication channel between processes:

    Queues
    ...
    Pipes
    ...

    '''
    kimchan
        4
    kimchan  
       2015-06-16 17:56:47 +08:00
    multiprocessing.Queue 是用来支持多个进程使用一个队列的, 如果你程序是多进程, 但是使用的队列的Queue.Queue, 那么每个进程就会都新建了一个队列, 无法供应资源
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3309 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:28 · PVG 22:28 · LAX 07:28 · JFK 10:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.