Kombu-python - 强制阻止/同步行为(或仅在上一次完成时处理消息)

时间:2014-03-01 19:06:51

标签: python kombu

我有Kombu处理rabbitmq队列并调用django函数/管理命令等。我的问题是我对正确的执行顺序有绝对的要求。在消息1和2的处理程序完成之前,消息3的tha处理程序永远不会运行。我需要确保Kombu在处理完前一个消息之前不处理另一条消息:

考虑这个基类

class UpdaterMixin(object):
    #  binding management commands to event names
    #  override in subclass
    event_handlers = {}
    app_name = ''   #override in subclass

    def __init__(self):
        if not self.app_name or len(self.event_handlers) == 0:
            print('app_name or event_handlers arent implemented')
            raise NotImplementedError()
        else:
            self.connection_url = settings.BROKER_URL
            self.exchange_name = settings.BUS_SETTINGS['exchange_name']
            self.exchange_type = settings.BUS_SETTINGS['exchange_type']
            self.routing_key = settings.ROUTING_KEYS[self.app_name]

    def start_listener(self):
        logger.info('started %s updater listener' % self.app_name)\\
        with Connection(self.connection_url) as connection:
            exchange = Exchange(self.exchange_name, self.exchange_type, durable=True)
            queue = Queue('%s_updater' % self.app_name, exchange=exchange, routing_key=self.routing_key)
            with connection.Consumer(queue, callbacks=[self.process_message]) as consumer:
                while True:
                    logger.info('Consuming events')
                    connection.drain_events()

    def process_message(self, body, message):


        logger.info('data received: %s' % body)
        handler = self.event_handlers[body['event']]
        logger.info('Executing management command: %s' % str(handler))
        data = json.dumps(body)
        call_command(handler, data, verbosity=3, interactive=False)
        message.ack()

有没有办法强制kombu这种行为?我不关心锁是否会在处理完成之前没有耗尽另一个事件,或者在前一个完成之前没有运行另一个process_message,或者任何其他方法来实现此操作。我只需要确保严格执行执行顺序。

我很乐意为此提供任何帮助。

1 个答案:

答案 0 :(得分:0)

因为默认情况下python是单线程的,所以默认情况下这个代码是阻塞/同步的,除非我明确地将它重写为异步。如果有人碰到这个