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()

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,857評論 2 45
  • # Python 資源大全中文版 我想很多程序員應(yīng)該記得 GitHub 上有一個 Awesome - XXX 系列...
    aimaile閱讀 26,832評論 6 427
  • 第6天·21天告別拖延 #玩卡不卡·每日一抽#每一位都可以通過這張卡片覺察自己: 1、直覺他叫什么名字?小妮 2、...
    龍女三娘閱讀 164評論 0 0
  • 看《鋼琴師》的時候,印象最深刻的一個片段是,鋼琴家Szpilman在四處躲藏納粹時,曾住進曾是相愛的心上人、如今已...
    淑子是永動機閱讀 310評論 0 0
  • 引子 作為每一個剛經(jīng)歷過高考的小伙伴應(yīng)該對浙江省的作文命題有所了解吧:VR(虛擬現(xiàn)實)現(xiàn)在已經(jīng)逐漸走進我們的生活,...
    KalamataTech閱讀 287評論 0 1

友情鏈接更多精彩內(nèi)容