码迷,mamicode.com
首页 > 编程语言 > 详细

练习--python中的Queue与多进程(multiprocessing)

时间:2015-11-15 23:05:50      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

按官方说法:

This module is OBSOLETE and is only provided on PyPI to support old projects that still use it. Please DO NOT USE IT FOR NEW PROJECTS! Use modern alternatives like the multiprocessing module in the standard library or even an asynchroneous approach with asyncio

threadpool 这个东东不推荐使用,那就跟上潮流,用用新的吧。

http://my.oschina.net/yangyanxing/blog/296052

锁之类的,POOL,MANAGER, LOCK,QUEUE之类,一应俱全。

# coding=utf8
from multiprocessing import Process, Queue, Manager, Pool
import multiprocessing
import os, time, random

def write(q, lock):
    lock.acquire()
    for value in [‘A‘, ‘B‘, ‘C‘]:
        print ‘Put %s to queue...‘ % value
        q.put(value)
        #time.sleep(random.random())
    lock.release()

def read(q):
    while True:
        if not q.empty():
            value = q.get(False)
            print ‘Get %s from queue.‘ % value
            time.sleep(random.random())
        else:
            break
        
                                           
if __name__ == ‘__main__‘:
    manager = multiprocessing.Manager()
    q = manager.Queue()
    lock = manager.Lock()
    p = Pool()
    pw = p.apply_async(write, args=(q, lock))
    time.sleep(0.5)
    pr = p.apply_async(read, args=(q,))
    p.close()
    p.join()

    print ‘all data write and read finished.‘

  技术分享

练习--python中的Queue与多进程(multiprocessing)

标签:

原文地址:http://www.cnblogs.com/aguncn/p/4967564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!