Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python multithreaded Queue module

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report