benjamin-hodgson / asynqp

An AMQP library for asyncio
MIT License
84 stars 29 forks source link

Issue using partials in `Queue.consume()` #92

Open bstempi opened 7 years ago

bstempi commented 7 years ago

Hi, I have some consumer callback function that looks like this:

def consume(msg, something_extra):
    print('Message: {}, extra context: {}'.format(msg.body, something_extra))

Somewhere else, I have something like this:

consumer_partial = functools.partial(consume, something_extra='xyz')
consumer = yield from queue.consume(consumer_partial)

For whatever reason, the callback is never invoked. If I pass a regular function, it works. It does not seem to work with a partial and I get no exceptions.

Any ideas?

bstempi commented 7 years ago

Update: This seems to happen if the function that I'm trying to modify is within a class. For example:

class Something(object):

    queue = magica_queue()

    def setup_consumer(self):
        def consume(msg):
            print(msg.body)

        consumer = yield from queue.consume(consume)

This also fails.

romantolkachyov commented 7 years ago

Seems like you missed something in handler declaration (I'm failed with self when I've got errors in this case). My working code:

    async def handle_message(self, message):  # it is a handler for other queue in my case
        handler = functools.partial(self.app.delivery_handler, queue.name)
        await queue.consume(handler)
    def delivery_handler(self, queue_name, message):
        ...