SoFunction
Updated on 2024-11-15

python queue Queue in detail

Queue

Queue is a thread-safe implementation of FIFO in the python standard library that provides a first-in-first-out data structure, or queue, for multi-threaded programming that is used to transfer information between producer and consumer threads.

Basic FIFO queue

class (maxsize=0)

FIFO that is, First in First Out, FIFO. Queue provides a basic FIFO container, the use of very simple, maxsize is an integer that specifies the upper limit of the number of data that can be stored in the queue. Once the upper limit is reached, insertion will result in blocking until the data in the queue is consumed. If maxsize is less than or equal to 0, the queue size is not limited.

Take a chestnut:

import Queue

q = ()

for i in range(5):
  (i)

while not ():
  print ()

Output:

0
1
2
3
4

LIFO queue

class (maxsize=0)

LIFO is Last in First Out, Last in First Out. Similar to the stack, the use is also very simple, maxsize usage is the same as above.

Another chestnut:

import Queue

q = ()

for i in range(5):
  (i)

while not ():
  print ()

Output:

4
3
2
1
0

You can see that simply replacing the class with the class

Priority Queue

class (maxsize=0)

Constructs a priority queue. maxsize is used as above.

import Queue
import threading

class Job(object):
  def __init__(self, priority, description):
     = priority
     = description
    print 'Job:',description
    return
  def __cmp__(self, other):
    return cmp(, )

q = ()

(Job(3, 'level 3 job'))
(Job(10, 'level 10 job'))
(Job(1, 'level 1 job'))

def process_job(q):
  while True:
    next_job = ()
    print 'for:', next_job.description
    q.task_done()

workers = [(target=process_job, args=(q,)),
    (target=process_job, args=(q,))
    ]

for w in workers:
  (True)
  ()

()

in the end

Job: level 3 job
Job: level 10 job
Job: level 1 job
for: level 1 job
for: level 3 job
for: job: level 10 job

Some common methods

task_done()

Means that one of the tasks previously entered into the queue has completed. Called by the queue's consumer thread. Each get() call gets a task, and the next task_done() call tells the queue that the task has been processed.

If a join() is currently blocking, it will resume execution when all the tasks in the queue have been processed (i.e., there is a corresponding task_done() call for each task queued by a put() call).

join()

Blocks the calling thread until all tasks in the queue have been processed.

The number of unfinished tasks increases whenever data is added to the queue. When the consumer thread calls task_done() (meaning that a consumer obtains the task and completes it), the number of unfinished tasks decreases. When the number of unfinished tasks drops to 0, join() unblocks.

put(item[, block[, timeout]])

Put the item into the queue.

  1. If the optional parameter block is True and timeout is an empty object (the default case, blocking calls with no timeout).
  2. If timeout is a positive integer, blocks the calling process for up to timeout seconds, and throws a Full exception (blocking call with timeout) if there has been no empty space available.
  3. If block is False, put the data into the queue if there is free space available, otherwise throw a Full exception immediately.
  4. Its non-blocking version is put_nowait equivalent to put(item, False)

get([block[, timeout]])

Removes a data from the queue and returns it. block is the same as the put method with the timeout parameter.

Its non-blocking method is 'get_nowait()' which is equivalent to get(False).

empty()

If the queue is empty, return True, otherwise return False.

The above is a small introduction to the python queue Queue detailed integration, I hope to help you, if you have any questions please leave me a message, I will reply to you in a timely manner. Here also thank you very much for your support of my website!