Common Usage of Python Multithreaded Queue Module

  • 2021-11-13 08:37:12
  • OfStack

Introduction to queue

queue is a standard library in python, commonly known as queue, which can be directly referenced by import. In python2.x, the module name is Queue
In python, the data between multiple threads is shared. When multiple threads exchange data, the security and uniformity of data cannot be guaranteed. Therefore, when multiple threads need to exchange data, queues appear. Queues can perfectly solve the data exchange between threads and ensure the security and uniformity of data between threads

Synchronized, thread-safe queue classes are provided in the Queue module of Python, including FIFO (first in first out) queue Queue, LIFO (last in first out) queue LifoQueue, and priority queue PriorityQueue.

These queues all implement lock primitives, which can be used directly in multithreading, and queues can be used to synchronize between threads.
Common methods in Queue module:

Queue. qsize () Returns the size of the queue
Queue. empty () returns True if the queue is empty, and False if the queue is empty
Queue. full () If the queue is full, return True, otherwise 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 until the queue is empty before doing anything else


import threading
import time

def a():
    print("a start\n")
    for i in range(10):
        time.sleep(0.1)
    print("a finish\n")
def b():
    print("b start\n")
    print("b finish\n")
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\n")
if __name__ == '__main__':
    main()

Queue Module:


import queue
import threading
import time

exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, q):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.q = 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 = 1

#  Create a new thread 
for tName in threadList:
    thread = myThread(threadID, tName, workQueue)
    thread.start()
    threads.append(thread)
    threadID += 1

#  Fill the queue 
queueLock.acquire()
for word in nameList:
    workQueue.put(word)
queueLock.release()

#  Wait for the queue to empty 
while not workQueue.empty():
    pass

#  Notify the thread that it is time to exit 
exitFlag = 1

#  Wait for all threads to complete 
for t in threads:
    t.join()
print (" Exit the main thread ")

Related articles: