Closures is a solution to keep the code async:
def declare_exchanges(self):
exchange = "exchangeA"
callback = self.on_exchange(exchange)
channel.exchange_declare(callback=callback, exchange=exchange)
exchange = "exchangeB"
callback = self.on_exchange(exchange)
channel.exchange_declare(callback=callback, exchange=exchange)
def on_exchange(self, exchange):
def callback(response):
print(exchange)
return callback
PS: I opened an issue upstream, https://github.com/pika/pika/issues/898