12.3 线程间通信

    从一个线程向另一个线程发送数据最安全的方式可能就是使用 库中的队列了。创建一个被多个线程共享的 Queue 对象,这些线程通过使用 put()get() 操作来向队列中添加或者删除元素。例如:

    Queue 对象已经包含了必要的锁,所以你可以通过它在多个线程间多安全地共享数据。当使用队列时,协调生产者和消费者的关闭问题可能会有一些麻烦。一个通用的解决方法是在队列中放置一个特殊的值,当消费者读到这个值的时候,终止执行。例如:

    1. from queue import Queue
    2. from threading import Thread
    3.  
    4. # Object that signals shutdown
    5. _sentinel = object()
    6.  
    7. # A thread that produces data
    8. def producer(out_q):
    9. while running:
    10. # Produce some data
    11. ...
    12. out_q.put(data)
    13.  
    14. # Put the sentinel on the queue to indicate completion
    15. out_q.put(_sentinel)
    16.  
    17. # A thread that consumes data
    18. def consumer(in_q):
    19. while True:
    20. # Get some data
    21. data = in_q.get()
    22.  
    23. # Check for termination
    24. if data is _sentinel:
    25. in_q.put(_sentinel)
    26. break
    27.  
    28. # Process the data
    29. ...

    本例中有一个特殊的地方:消费者在读到这个特殊值之后立即又把它放回到队列中,将之传递下去。这样,所有监听这个队列的消费者线程就可以全部关闭了。尽管队列是最常见的线程间通信机制,但是仍然可以自己通过创建自己的数据结构并添加所需的锁和同步机制来实现线程间通信。最常见的方法是使用 Condition 变量来包装你的数据结构。下边这个例子演示了如何创建一个线程安全的优先级队列,如同1.5节中介绍的那样。

    1. import heapq
    2. import threading
    3.  
    4. def __init__(self):
    5. self._queue = []
    6. self._count = 0
    7. self._cv = threading.Condition()
    8. def put(self, item, priority):
    9. with self._cv:
    10. heapq.heappush(self._queue, (-priority, self._count, item))
    11. self._count += 1
    12. self._cv.notify()
    13.  
    14. def get(self):
    15. with self._cv:
    16. while len(self._queue) == 0:
    17. self._cv.wait()
    18. return heapq.heappop(self._queue)[-1]

    如果一个线程需要在一个“消费者”线程处理完特定的数据项时立即得到通知,你可以把要发送的数据和一个 Event 放到一起使用,这样“生产者”就可以通过这个Event对象来监测处理的过程了。示例如下:

    1. from queue import Queue
    2. from threading import Thread, Event
    3.  
    4. # A thread that produces data
    5. def producer(out_q):
    6. while running:
    7. # Produce some data
    8. ...
    9. # Make an (data, event) pair and hand it to the consumer
    10. evt = Event()
    11. out_q.put((data, evt))
    12. ...
    13. # Wait for the consumer to process the item
    14. evt.wait()
    15.  
    16. # A thread that consumes data
    17. def consumer(in_q):
    18. while True:
    19. # Get some data
    20. data, evt = in_q.get()
    21. ...
    22. # Indicate completion
    23. evt.set()

    基于简单队列编写多线程程序在多数情况下是一个比较明智的选择。从线程安全队列的底层实现来看,你无需在你的代码中使用锁和其他底层的同步机制,这些只会把你的程序弄得乱七八糟。此外,使用队列这种基于消息的通信机制可以被扩展到更大的应用范畴,比如,你可以把你的程序放入多个进程甚至是分布式系统而无需改变底层的队列结构。使用线程队列有一个要注意的问题是,向队列中添加数据项时并不会复制此数据项,线程间通信实际上是在线程间传递对象引用。如果你担心对象的共享状态,那你最好只传递不可修改的数据结构(如:整型、字符串或者元组)或者一个对象的深拷贝。例如:

    1. from queue import Queue
    2. from threading import Thread
    3. import copy
    4.  
    5. # A thread that produces data
    6. def producer(out_q):
    7. while True:
    8. # Produce some data
    9. ...
    10. out_q.put(copy.deepcopy(data))
    11.  
    12. # A thread that consumes data
    13. def consumer(in_q):
    14. while True:
    15. # Get some data
    16. data = in_q.get()
    17. # Process the data
    18. ...

    Queue 对象提供一些在当前上下文很有用的附加特性。比如在创建 Queue 对象时提供可选的 size 参数来限制可以添加到队列中的元素数量。对于“生产者”与“消费者”速度有差异的情况,为队列中的元素数量添加上限是有意义的。比如,一个“生产者”产生项目的速度比“消费者” “消费”的速度快,那么使用固定大小的队列就可以在队列已满的时候阻塞队列,以免未预期的连锁效应扩散整个程序造成死锁或者程序运行失常。在通信的线程之间进行“流量控制”是一个看起来容易实现起来困难的问题。如果你发现自己曾经试图通过摆弄队列大小来解决一个问题,这也许就标志着你的程序可能存在脆弱设计或者固有的可伸缩问题。get()put() 方法都支持非阻塞方式和设定超时,例如:

    1. def producer(q):
    2. ...
    3. try:
    4. q.put(item, block=False)
    5. except queue.Full:
    6. log.warning('queued item %r discarded!', item)

    如果你试图让消费者线程在执行像 这样的操作时,超时自动终止以便检查终止标志,你应该使用 q.get() 的可选参数 timeout ,如下:

    1. _running = True
    2.  
    3. def consumer(q):
    4. while _running:
    5. try:
    6. item = q.get(timeout=5.0)
    7. # Process item
    8. ...
    9. pass

    最后,有 q.qsize()q.full()q.empty() 等实用方法可以获取一个队列的当前大小和状态。但要注意,这些方法都不是线程安全的。可能你对一个队列使用 empty() 判断出这个队列为空,但同时另外一个线程可能已经向这个队列中插入一个数据项。所以,你最好不要在你的代码中使用这些方法。

    原文: