Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Python multithreaded Queue module

Shulou Source: shulou.com Published: 2022-06-01 02:51:32 10月03日 Update

This article introduces the relevant knowledge of "how to use the Python multithreaded Queue module". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Queue introduction

Queue is a standard library in python, commonly known as queue, which can be referenced directly by import. In python2.x, the module is named Queue.

In python, the data between multiple threads is shared, and when multiple threads exchange data, the security and consistency of the data cannot be guaranteed, so when multiple threads need to exchange data, the queue appears. The queue can perfectly solve the data exchange between threads and ensure the security and consistency of data between threads.

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.

Import threadingimport timedef a (): print ("a start") for i in range (10): time.sleep (0.1) print ("a finish") def b (): print ("b start") print ("b finish") def main (): # t=threading.Thread (target=a Name= "T") t = threading.Thread (target=a) t2=threading.Thread (target=b) t.start () t2.start () # t2.join () # t.join () print ("all done") if _ _ name__ = = "_ _ main__": main ()

Queue module:

Import 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 complete for t in threads: t.join () print ("exit main thread") "Python multiline" This is the end of the content of how to use the program Queue module. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Threads queues modules data security multiple time consistency consistency content size security actual time more knowledge first out guarantee synchronization practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Technology Apple MySQL Xiaomi