python queue

python queue

http://www.learn4master.com/programming-language/python/python-queue-for-multithreading
http://link.zhihu.com/?target=http%3A//www.dongwm.com/archives/%25E4%25BD%25BF%25E7%2594%25A8Python%25E8%25BF%259B%25E8%25A1%258C%25E5%25B9%25B6%25E5%258F%2591%25E7%25BC%2596%25E7%25A8%258B-%25E7%25BA%25BF%25E7%25A8%258B%25E7%25AF%2587/

import threading
import time
import queue


def consume(q):
    while True:
        name = threading.current_thread().getName()
        print("Thread: {0} start get item from queue[current size = {1}] at time = {2} \n".format(name, q.qsize(),
                                                                                                  time.strftime(
                                                                                                      '%H:%M:%S')))
        item = q.get()
        time.sleep(3)
        print("Thread: {0} finish process item from queue[current size = {1}] at time = {2} \n".format(name, q.qsize(),
                                                                                                       time.strftime(
                                                                                                           '%H:%M:%S')))
        q.task_done()


def producer(q):
    for i in range(10):
        name = threading.current_thread().getName()
        print("Thread: {0} start put item into queue[current size = {1}] at time = {2} \n".format(name, q.qsize(),
                                                                                                  time.strftime(
                                                                                                      '%H:%M:%S')))
        item = "item-" + str(i)
        q.put(item)
        print("Thread: {0} successfully put item into queue[current size = {1}] at time = {2} \n".format(name, q.qsize(),
                                                                                                       time.strftime(
                                                                                                           '%H:%M:%S')))
    q.join()


if __name__ == '__main__':
    q = queue.Queue(maxsize=3)
    # 3 threads to consume
    threads_num = 3
    for i in range(threads_num):
        t = threading.Thread(name="consumerThread-" + str(i), target=consume, args=(q,))
        t.start()
    # thread to produce
    t = threading.Thread(name="produceThread", target=producer, args=(q,))
    t.start()
    # q.join()

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,724评论 2 45
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,721评论 6 427
  • 第6天·21天告别拖延 #玩卡不卡·每日一抽#每一位都可以通过这张卡片觉察自己: 1、直觉他叫什么名字?小妮 2、...
    龙女三娘阅读 994评论 0 0
  • 看《钢琴师》的时候,印象最深刻的一个片段是,钢琴家Szpilman在四处躲藏纳粹时,曾住进曾是相爱的心上人、如今已...
    淑子是永动机阅读 1,924评论 0 0
  • 引子 作为每一个刚经历过高考的小伙伴应该对浙江省的作文命题有所了解吧:VR(虚拟现实)现在已经逐渐走进我们的生活,...
    KalamataTech阅读 1,781评论 0 1