Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to understand Python3 thread priority queue Queue

Shulou Source: shulou.com Published: 2022-06-01 03:18:54 10月03日 Update

In this issue, the editor will bring you about how to understand the Python3 thread priority queue Queue. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Python's Queue module provides synchronous, thread-safe queue classes, including FIFO (first-in, first-out) queues, Queue,LIFO (last-in, first-out) queues, LifoQueue, and priority queues, PriorityQueue.

These queues implement lock primitives, can be used directly in multithreading, and can use queues to achieve synchronization between threads.

Common methods in the Queue module:

Queue.qsize () returns the size of the queue

Queue.empty () returns True if the queue is empty, and vice versa False

Queue.full () returns True if the queue is full, and vice versa False

Queue.full corresponds to maxsize size

Queue.get ([block [, timeout]]) get queue, timeout wait time

Queue.get_nowait () is equivalent to Queue.get (False)

Queue.put (item) write queue, timeout wait time

Queue.put_nowait (item) is equivalent to Queue.put (item, False)

Queue.task_done () after completing a task, the Queue.task_done () function sends a signal to the queue where the task has been completed.

Queue.join () actually means waiting for the queue to be empty before performing anything else.

Example:

#! / usr/bin/python3import queueimport threadingimport timeexitFlag = 0class myThread (threading.Thread): def _ _ init__ (self, threadID, name, Q): threading.Thread.__init__ (self) self.threadID = threadID self.name = q def run (self): print ("Open thread:" + self.name) process_data (self.name) Self.q) print ("exit thread:" + self.name) def process_data (threadName, Q): while not exitFlag: queueLock.acquire () if not workQueue.empty (): data = q.get () queueLock.release () print ("% s processing% s"% (threadName) Data) else: queueLock.release () time.sleep (1) threadList = ["Thread-1", "Thread-2", "Thread-3"] nameList = ["One", "Two", "Three", "Four", "Five"] queueLock = threading.Lock () workQueue = queue.Queue (10) threads = [] threadID = create a new thread for tName in threadList: thread = myThread (threadID, tName) WorkQueue) thread.start () threads.append (thread) threadID + = filling queue queueLock.acquire () for word in nameList: workQueue.put (word) queueLock.release () # waiting queue emptying while not workQueue.empty (): pass# notifies thread that it is time to exit exitFlag = waiting for all threads to finish for t in threads: t.join () print ("exit main thread")

The execution results of the above procedures:

Open thread: Thread-1 open thread: Thread-2 open thread: Thread-3Thread-3 processing OneThread-1 processing TwoThread-2 processing ThreeThread-3 processing FourThread-1 processing Five exit thread: Thread-3 exit thread: Thread-2 exit thread: Thread-1 exit main thread. This is how to understand the Python3 thread priority queue Queue. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Tags: Threads queues priorities content size time modules first out analysis synchronization security professional small and medium tasks signals rich content functions primitives instances reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi Microsoft OPPO Reno Huawei