Home  >  Article  >  Backend Development  >  Comprehensive analysis of the principle of python thread priority queue (queue)

Comprehensive analysis of the principle of python thread priority queue (queue)

Tomorin
TomorinOriginal
2018-08-16 17:38:243103browse

Python's Queue module provides synchronous, thread-safe queue classes, including FIFO (first in first out) queue Queue, LIFO (last in first out) queue LifoQueue, and priority queue PriorityQueue . These queues implement lock primitives and can be used directly in multi-threads. Queues can be used to achieve synchronization between threads.

Commonly used methods in the Queue module:

1.Queue.qsize() returns the size of the queue

2.Queue.empty() If the queue is empty, returns True , Otherwise False

3.Queue.full() If the queue is full, return True, otherwise False

4.Queue.full corresponds to the maxsize size

5.Queue .get([block[, timeout]]) gets the queue, timeout waiting time

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

7.Queue.put(item ) Write to the queue, timeout waiting time

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

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

10.Queue.join() actually means waiting until the queue is empty before performing other operations

#!/usr/bin/python
# -*- coding: UTF-8 -*-
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 "Starting " + self.name
process_data(self.name, self.q)
print "Exiting " + 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
# 创建新线程
for tName in threadList:
thread = myThread(threadID, tName, workQueue)
thread.start()
threads.append(thread)
threadID += 1
# 填充队列
queueLock.acquire()
for word in nameList:
workQueue.put(word)
queueLock.release()
# 等待队列清空
while not workQueue.empty():
pass
# 通知线程是时候退出
exitFlag = 1
# 等待所有线程完成
for t in threads:
t.join()
print "Exiting Main Thread"

The execution result of the above program:

Starting Thread-1
Starting Thread-2
Starting Thread-3
Thread-1 processing One
Thread-2 processing Two
Thread-3 processing Three
Thread-1 processing Four
Thread-2 processing Five
Exiting Thread-3
Exiting Thread-1
Exiting Thread-2
Exiting Main Thread


The above is the detailed content of Comprehensive analysis of the principle of python thread priority queue (queue). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn