如何在线程内运行的芹菜任务作为RabbitMQ Consumer回调执行?

时间:2019-06-05 15:03:57

标签: python multithreading rabbitmq celery

这个想法是: 我有一个在线程中运行的kombu.mixins.ConsumerMixin对象。对于不同的主题,我需要不同的消费者,每个消费者执行不同的芹菜任务。因此,我想将一个celery任务传递给线程以充当使用者回调。

TopicConsumer类:

class TopicConsumer(ConsumerMixin):
    def __init__(self, connection, exchange_name, binding_keys):
        super().__init__()
        self.connection = connection
        self.exchange = Exchange(exchange_name, type='topic')

        self.q_bindings = [binding(self.exchange, routing_key=k) for k in binding_keys]
        self.q_name = f"{exchange_name}:{'|'.join(str(key) for key in binding_keys)}"
        self.queue = Queue(name=self.q_name,
                           exchange=self.exchange,
                           bindings=self.q_bindings)

    def get_consumers(self, Consumer, channel):
        consumers = list()
        consumer = Consumer(queues=self.queue,
                            accept=['json'],
                            callbacks=[self._handle_message])
        consumers.append(consumer)
        return consumers

    def _handle_message(self, body, message):
        try: 
            message.ack()
        except MessageStateError:
            # message has already been processed, rejected or requeued
            pass
        else:
            self.process_message(message.payload)

    def process_message(self, data):
        '''Must be overwritten by subclass.'''
        raise NotImplementedError('Subclass responsibility.')


MessageConsumerThread:

import threading
from types import MethodType

from .TopicConsumer import TopicConsumer


class MessageConsumerThread(threading.Thread):
    def __init__(self, connection, exchange_name, binding_keys, msg_handler=None):
        super().__init__()
        self.connection = connection
        self.exchange_name = exchange_name
        self.binding_keys = binding_keys

        if msg_handler:
            self.register_message_handler(msg_handler)

    def register_message_handler(self, msg_handler):
        TopicConsumer.process_message = MethodType(msg_handler, TopicConsumer)
        self._create_consumer()

    def _create_consumer(self):
        self.consumer = TopicConsumer(self.connection, self.exchange_name, self.binding_keys)

    def run(self):
        self.consumer.run()

    def stop(self):
        self.consumer.should_stop = True

在主应用程序中,有一个celery_task由函数调用。

def msg_handler(data):
    celery_task.delay(data)

我想使用msg_handlerTopicConsumer.process_message()注册为消息处理回调types.MethodType(我也尝试过setattr。也不起作用。)

现在我以这种方式尝试过:

with Connection(**connection_parameters) as conn:
    msg_consumer = MessageConsumerThread(conn,
                                         exchange_name='messenger',
                                         binding_keys=['key'])
    msg_consumer.register_message_handler(msg_handler)
    msg_consumer.start()

如果按照所示方式执行操作,则我可以从其中执行msg_handler MessageConsumerThread个对象。但是它不是从TopicConsumer对象内部执行的,因为有需要处理的消息正文,因此我在需要它的地方执行它。现在我已经知道如何运行它。

您有什么建议吗?预先感谢!

0 个答案:

没有答案