欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Python程序中的線程操作-線程隊列

系統 1642 0

目錄

  • 一、線程隊列
  • 二、先進先出
  • 三、后進先出
  • 四、存儲數據時可設置優先級的隊列
    • 4.1 優先級隊列
    • 4.2 更多方法說明

一、線程隊列

queue隊列:使用 import queue ,用法與進程Queue一樣

queue is especially useful in threaded programming when information must be exchanged safely between multiple threads.

二、先進先出

class queue.Queue(maxsize=0)

          
            import queue

q=queue.Queue()
q.put('first')
q.put('second')
q.put('third')

print(q.get())
print(q.get())
print(q.get())
'''
結果(先進先出):
first
second
third
'''
          
        

三、后進先出

class queue.LifoQueue(maxsize=0)

          
            import queue

q=queue.LifoQueue()
q.put('first')
q.put('second')
q.put('third')

print(q.get())
print(q.get())
print(q.get())
'''
結果(后進先出):
third
second
first
'''
          
        

四、存儲數據時可設置優先級的隊列

class queue.PriorityQueue(maxsize=0)

4.1 優先級隊列

          
            import queue

q=queue.PriorityQueue()
#put進入一個元組,元組的第一個元素是優先級(通常是數字,也可以是非數字之間的比較),數字越小優先級越高
q.put((20,'a'))
q.put((10,'b'))
q.put((30,'c'))

print(q.get())
print(q.get())
print(q.get())
'''
結果(數字越小優先級越高,優先級高的優先出隊):
(10, 'b')
(20, 'a')
(30, 'c')
'''
          
        

4.2 更多方法說明

Constructor for a priority queue. maxsize is an integer that sets the upperbound limit on the number of items that can be placed in the queue. Insertion will block once this size has been reached, until queue items are consumed. If maxsize is less than or equal to zero, the queue size is infinite.

The lowest valued entries are retrieved first (the lowest valued entry is the one returned by sorted(list(entries))[0]) . A typical pattern for entries is a tuple in the form: (priority_number, data).

exception queue.Empty : Exception raised when non-blocking get() (or get_nowait()) is called on a Queue object which is empty.

exception queue.Full : Exception raised when non-blocking put() (or put_nowait()) is called on a Queue object which is full.

Queue.qsize()

Queue.empty() : return True if empty

Queue.full() : return True if full

Queue.put(item, block=True, timeout=None) : Put item into the queue. If optional args block is true and timeout is None (the default), block if necessary until a free slot is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Full exception if no free slot was available within that time. Otherwise (block is false), put an item on the queue if a free slot is immediately available, else raise the Full exception (timeout is ignored in that case).

Queue.put_nowait(item) : Equivalent to put(item, False).

Queue.get(block=True, timeout=None) : Remove and return an item from the queue. If optional args block is true and timeout is None (the default), block if necessary until an item is available. If timeout is a positive number, it blocks at most timeout seconds and raises the Empty exception if no item was available within that time. Otherwise (block is false), return an item if one is immediately available, else raise the Empty exception (timeout is ignored in that case).

Queue.get_nowait() : Equivalent to get(False).

Two methods are offered to support tracking whether enqueued tasks have been fully processed by daemon consumer threads.

Queue.task_done() : Indicate that a formerly enqueued task is complete. Used by queue consumer threads. For each get() used to fetch a task, a subsequent call to task_done() tells the queue that the processing on the task is complete.

If a join() is currently blocking, it will resume when all items have been processed (meaning that a task_done() call was received for every item that had been put() into the queue).

Raises a ValueError if called more times than there were items placed in the queue.

Queue.join() : block直到queue被消費完畢。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久免费99精品久久久久久 | 成人国产免费观看 | 国产精品视频观看 | 日本污污视频在线观看 | 欧美黄色免费网站 | 亚洲欧美中文日韩在线v日本 | 国精品日韩欧美一区二区三区 | 大伊香蕉精品视频在线天堂 | 九九精品九九 | 欧美成人观看视频在线 | 初女破苞国语在线观看免费 | 国产一级做人爱c黑人版 | 看片国产| jizzzxxxxhd | 国产精品视_精品国产免费 亚洲综合在线另类色区奇米 | 久久精品无码一区二区日韩av | 成人精品视频一区二区三区尤物 | 一区二区三区国产 | 国产日韩一区二区三免费高清 | 久久aⅴ国产欧美74aaa | 亚洲乱码视频在线观看 | 免费黄色大片在线观看 | 国产精品福利在线观看免费不卡 | 天天操综合 | 亚洲精品视频在线 | 国产网站免费视频 | 国产短视频精品区第一页 | 国产精品久久久久久久久免费 | 国产久| 国产99精品一区二区三区免费 | 日韩精品一区二区三区不卡 | 污片免费看 | 久久精品麻豆 | 亚洲精品97福利在线 | 国产日产亚洲欧美综合另类 | 国产亚洲精品不卡在线 | 国产亚洲精品久久久久久老妇 | 成人网址大全 | 色婷婷综合网 | 欧美日韩综合精品一区二区三区 | 亚洲品质自拍视频 |