Get the App
SLTechnology News&Howtos  ›  Development  › 

The usage of queue queue Module under Python Thread

Shulou Source: shulou.com Published: 2022-06-03 05:59:46 10月13日 Update

The usage of queue queue module under Python thread. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

The queue module implements various [multi-producer-multi-consumer] queues. Can be used to safely exchange information between multiple threads of execution.

The queue module defines three different queue classes.

Three different queue classes

Q=Queue (maxsize): create a FIFO (first-in first-out, first in first out) queue. Maxsize is the maximum number of items in the queue for which the amount is placed.

If you omit the maxsize parameter or set it to 0, the queue size will be infinite.

Q=LifoQueue (maxsize): create a LIFO (last-in first-out, LIFO) queue (stack).

Q=PriorityQueue (maxsize): creates a priority queue in which items are arranged in order of priority from lowest to highest.

When using such a queue, the item should be a tuple in the form of (priority,data), where priority is a number that identifies priority.

Common methods

Q.size (): returns the correct size of the queue. Because other threads may be updating this queue, the number returned by this method is unreliable.

Q.empty (): returns True if the queue is empty, False otherwise.

Q.full (): returns True if the queue is full, False otherwise.

Q.put (item,block,timeout): put item in the queue.

If block is set to True (the default), the caller will be blocked until there is an available free place in the queue.

If block is set to False, this method throws a Full exception when the queue is full.

Q.put_nowait (item): equivalent to q.put (item,False)

Q.get (block,timeout): removes an item from the queue and returns it.

If block is set to True (the default), the caller will block until there is free time available in the queue.

If block is set to False, an Empty exception is thrown when the queue is empty.

Timeout provides an optional timeout value in seconds, and if it times out, an Empty exception is thrown.

Q.get_nowait (): equivalent to get (0)

Q.task_done (): consumers who plant data in the queue are used to indicate that the processing of the item has been completed. If you use this method, each item deleted from the queue should be called once.

Q.join (): blocks until all items in the queue are deleted and processed. Once the q.task_done () method is called once for each item in the queue, this method will return directly.

Example:

Using queues can generally simplify multithreaded programs. For example, you can use shared queues to connect threads together without relying on lock protection.

In this model, worker threads generally act as consumers of data.

From threading import Thread

From queue import Queue

Class WorkerThread (Thread):

Def _ init__ (self,*args,**kwargs):

Thread.__init__ (self,*args,**kwargs)

Self.input_queue=Queue ()

Def send (self,item):

Self.input_queue.put (item)

Def close (self):

Self.input_queue.put (None)

Self.input_queue.join ()

Def run (self):

While True:

Item=self.input_queue.get ()

If item is None:

Break

# in actual development, useful work should be used instead of

Print (item)

Self.input_queue.task_done ()

# complete, instructions to receive and return to the sentinel

Self.input_queue.task_done ()

Return

If _ _ name__== "_ _ main__":

W=WorkerThread ()

W.start ()

W.send ("learn")

W.send ("step")

W.send ("Garden")

W.send ("net")

W.close ()

Running result:

To learn

Step

Garden

Net

This is the answer to the question about the usage of the queue queue module under Python threads. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Queues threads modules this method priority method consumer problem consumption blocking different size indication number data more user idle equivalent first out Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Docker MariaDB Redmi Shulou Technology