简体   繁体   English

Python - Queue究竟是如何工作的?

[英]Python - how exactly Queue works?

Regarding example from documentation: https://docs.python.org/2/library/queue.html#Queue.Queue.get 关于文档中的示例: https//docs.python.org/2/library/queue.html#Queue.Queue.get

def worker():
    while True:
        item = q.get()
        do_work(item)
        q.task_done()

How actually the worker would know all work is done, the queue is empty and we can exit? 实际上工人怎么知道所有的工作都已完成,队列是空的,我们可以退出? I don't understand it... 我不明白......

Your worker hangs in a while True: loop which means that the function/thread will never return. 你的工作程序会暂停一下while True:循环,这意味着函数/线程永远不会返回。

The "magic" lies in the code you didn't show: “魔力”在于你没有展示的代码:

 t = Thread(target=worker)
 t.daemon = True
 t.start()

The daemon parameter controls when the uppper thread can exit 守护进程参数控制超级线程何时可以退出

The entire Python program exits when no alive non-daemon threads are left. 当没有剩下活着的非守护程序线程时,整个Python程序退出。

Which means, that the program will exit, because the main thread exists. 这意味着程序将退出,因为主线程存在。 The worker thread thenically lives, but will be destroyed when the main thread ends (because "there are no non-daemon threads left"). 工作线程终于存在,但是当主线程结束时将被销毁(因为“没有留下非守护程序线程”)。

The main thread exit condition is 主线程退出条件是

q.join()

The documentatino for join will show, when it stops blocking the execution. join的documentatino将在停止阻止执行时显示。

[...] When the count of unfinished tasks drops to zero, join() unblocks. [...]当未完成任务的数量降至零时,join()取消阻止。

I'll keep it simple. 我会保持简单。 Queue is basically collections of items like list for instance, difference it doesn't allow random access of elements. 队列基本上是列表的集合,例如列表,差异,它不允许随机访问元素。 You insert and delete items in certain manner. 您以某种方式插入和删除项目。 The default type of queue is FIFO( first in first out). 队列的默认类型是FIFO(先进先出)。 As you can figure from its name, its like a normal queue you see at any supermarket(or any place) the first person who entered the line will leave first. 正如您可以从其名称中看到的那样,它就像您在任何超市(或任何地方)看到的正常队列一样,第一个进入该线路的人将首先离开。

There are three types of queue: 队列有三种类型:

  1. FIFO FIFO
  2. LIFO LIFO
  3. PRIORITY 优先

FIFO like i said has rule of first in first out: 像我说的FIFO有先进先出规则:

import queue #importing the library

q=queue.Queue() #create a queue object

for i in range(5):
    print(q.put(i)) #adding elements into our queue

while not q.empty():
    print(q.get()) #to delete item and printing it

LIFO works on the principle of last in first out: LIFO的工作原则是先进先出:

import queue #importing the library

q=queue.LifoQueue() #create a queue object

for i in range(5):
    print(q.put(i)) #adding elements into our queue

while not q.empty():
    print(q.get()) #to delete item and printing it

PRIORTY queue gives out data in ascending order, as in, the smallest one will exit the queue first. PRIORTY队列按升序排列数据,因为最小的队列将首先退出队列。

import queue #importing the library

q=queue.LifoQueue() #create a queue object


q.put(3) 
q.put(7) 
q.put(2) 
q.put(7) 
q.put(1) 


while not q.empty():
    print(q.get()) #to delete item and printing it

To answer your last question, as you can see in example, you can use q.empty() to check if your queue is empty or not. 要回答上一个问题,正如您在示例中所看到的,您可以使用q.empty()来检查您的队列是否为空。

If you have any further doubt feel free to ask. 如果您有任何疑问,请随时提出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM