Python的Queue模块提供了同步的、线程安全的队列类,包括FIFO(先入先出)队列Queue,LIFO(后入先出)队列LifoQueue,和优先级队列PriorityQueue。这些队列都实现了锁原语,能够在多线程中直接使用,可以使用队列来实现线程间的同步。
Queue模块中的常用方法:
- Queue.qsize():返回队列的大小
- Queue.empty():如果队列为空,返回True,反之False
- Queue.full():如果队列满了,返回True,反之False
- Queue.full 与 maxsize 大小对应
- Queue.get([block[, timeout]]):获取队列,timeout等待时间
- Queue.get_nowait():相当Queue.get(False)
- Queue.put(item):写入队列,timeout等待时间
- Queue.put_nowait(item):相当Queue.put(item, False)
- Queue.task_done():在完成一项工作之后,Queue.task_done()函数向任务已经完成的队列发送一个信号
- Queue.join():实际上意味着等到队列为空,再执行别的操作
接下来,我们将通过一个简单的示例来演示如何使用Queue模块实现线程间的同步。在这个示例中,我们将创建两个线程,分别执行a()和b()函数。这两个函数将共享一个队列,用于在它们之间传递数据。
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()
在这个示例中,我们首先导入了threading和time模块。然后,我们定义了两个函数a()和b(),它们分别打印一些信息并暂停一段时间。接下来,我们定义了一个main()函数,在这个函数中,我们创建了两个线程t和t2,分别执行a()和b()函数。最后,我们启动这两个线程,并等待它们完成。
现在,让我们来看一下如何使用Queue模块来实现线程间的同步。在这个示例中,我们将创建一个名为myThread的自定义线程类,它继承自threading.Thread类。在这个类中,我们将定义一个run()方法,这个方法将在线程启动时被调用。在run()方法中,我们将处理队列中的数据,并在完成后打印一条消息。
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 ("开启线程:" + self.name)
process_data(self.name, self.q)
print ("退出线程:" + 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()