mosquito / aio-pika

AMQP 0.9 client designed for asyncio and humans.
https://aio-pika.readthedocs.org/
Apache License 2.0
1.18k stars 186 forks source link

How to properly add callbacks? #555

Open K1T3K1 opened 1 year ago

K1T3K1 commented 1 year ago

I'm want to add Callbacks to my RobutConnection

TypeError: my_class.lost_connection_amqp_callback() takes 2 positional arguments but 3 were given

that's the method signature

async def lost_connection_amqp_callback(self, sender):

And thats how I add my callbacks

async def add_callbacks(self, connection):
  connection.close_callbacks.add(self.lost_connection_amqp_callback)  
  connection.reconnect_callbacks.add(self.reconnected_amqp_callback)

what am I doing wrong? Documentation says nothing about it

decaz commented 1 year ago

Close callback receives not only sender but exception too. Here you can see how it goes: https://github.com/mosquito/aio-pika/blob/4abca046d81656dc10de9fad8acbd654ced964db/aio_pika/connection.py#L86 https://github.com/mosquito/aio-pika/blob/4abca046d81656dc10de9fad8acbd654ced964db/aio_pika/channel.py#L205 But here I found it receives future with the exception set: https://github.com/mosquito/aio-pika/blob/4abca046d81656dc10de9fad8acbd654ced964db/aio_pika/robust_connection.py#L101-L104 @mosquito is it correct? Why do we need future here?

K1T3K1 commented 1 year ago

I added third arg to close callback and it is working