V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · May 18, 2015 · 4002 views
    This topic created in 3997 days ago, the information mentioned may be changed or developed.
    用法都一样, 但下面这段代码,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 replies    2015-06-16 17:56:47 +08:00
    clino
        1
    clino  
       May 18, 2015
    multiprocessing.Queue 这个重点是支持进程间的通讯
    第二个应该木有这种功能吧
    cc7756789
        2
    cc7756789  
    OP
       May 18, 2015
    @clino 我的理解就是Queue模块是给多线程用的,多线程之间可以共享内存,而multiprocessing自带的Queue支持多进程间的通讯 ?
    clino
        3
    clino  
       May 18, 2015
    @cc7756789
    文档不看的?
    '''
    multiprocessing supports two types of communication channel between processes:

    Queues
    ...
    Pipes
    ...

    '''
    kimchan
        4
    kimchan  
       Jun 16, 2015
    multiprocessing.Queue 是用来支持多个进程使用一个队列的, 如果你程序是多进程, 但是使用的队列的Queue.Queue, 那么每个进程就会都新建了一个队列, 无法供应资源
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   969 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 19:54 · PVG 03:54 · LAX 12:54 · JFK 15:54
    ♥ Do have faith in what you're doing.