In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.