简体   繁体   English

如何从python中的线程池中唤醒线程?

[英]How to wakeup thread from thread pool in python?

I new to Python and am developing an application in Python 2.7. 我是Python的新手,正在Python 2.7中开发应用程序。 I am using a thread pool provided by the concurrent.futures library. 我正在使用由concurrent.futures库提供的线程池。 Once a thread from ThreadPool is started, it needs to wait for some message from RabbitMQ. 一旦从ThreadPool启动线程,它就需要等待RabbitMQ发出一些消息。

How can I implement this logic in Python to make this thread from the pool wait for event messages? 如何在Python中实现此逻辑,以使池中的该线程等待事件消息? Basically I need to wake up a waiting thread once I receive message from RabbitMQ (ie wait and notify implementation on ThreadPool ). 基本上,一旦我收到RabbitMQ的消息(即,等待并通知ThreadPool实现),就需要唤醒一个等待线程。

First you define a Queue : 首先,您定义一个Queue

from Queue import Queue

q = Queue()

then, in your thread, you attempt to get an item from that queue: 然后,在您的线程中,尝试从该队列中获取一个项目:

msg = q.get()

this will block the entire thread until there is something to be found in the queue. 这将阻塞整个线程,直到在队列中找不到任何东西为止。

Now, at the same time, assuming your incoming events are notified by means of triggering callbacks, you register a callback that simply puts the received RabbitMQ message in the queue: 现在,同时,假设您的传入事件是通过触发回调来通知的,那么您将注册一个回调,该回调将接收到的RabbitMQ消息简单地放入队列中:

def on_message(msg):
    q.put(msg)
rabbitmq_channel.register_callback(on_message)

or if you like shorter code: 或者,如果您喜欢较短的代码:

rabbitmq_channel.register_callback(lambda msg: q.put(msg))

(the above is pseudocode because I've not used RabbitMQ nor whatever Python bindings for RabbitMQ, but you should be able to easily figure out how to adapt the snippet to your real application code; the key part to pay attention to is q.put(msg) —just make sure that part gets invoked as soon as a new message is notified.) (上面是伪代码,因为我没有使用RabbitMQ或RabbitMQ的任何Python绑定,但是您应该能够轻松地弄清楚如何使代码段适应您的实际应用程序代码;要注意的关键部分是q.put(msg)只要确保在收到新消息后立即调用该部分。)

as soon as this happens, the thread is awakened and is free to process the message. 一旦发生这种情况,线程将被唤醒并可以自由处理消息。 In order to reuse the same thread for multiple messages, just use a while loop: 为了将同一线程重用于多个消息,只需使用while循环:

while True:
    msg = q.get()
    process_message(msg)

PS I would suggest looking into Gevent and how to combine it with RabbitMQ in your Python application so as to be able to get rid of threads and use more lightweight and scalable green threading mechanism instead without ever having to manage a threadpool (because you can just have tens of thousands of greenlets spawned and killed on the fly): PS:我建议您研究Gevent以及如何将其与Python应用程序中的RabbitMQ结合使用,从而摆脱线程并使用更轻量和可扩展的绿色线程机制,而不必管理线程池(因为您可以有成千上万的小苍蝇在飞行中产卵并被杀死):

# this thing always called in a green thread; forget about pools and queues.
def on_message(msg):
    # you're in a green thread now; just process away!
    benefit_from("all the gevent goodness!")
    spawn_and_join_10_sub_greenlets()

rabbitmq_channel.register_callback(lambda msg: gevent.spawn(on_message, msg))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM