I have a callback called by a third-party library.
def callback(message):
print(message)
And I want to be able to do something like:
async for message in SomeIterator():
print(message)
I know I can do it using asyncio.Queue
like in Converting a Python function with a callback to an asyncio awaitable
The problem is that I can not properly clean up because there is no "exit" method for the async iterator. So there is no place for me to remove callback. The garbage collector will not collect my generator object because someone stores a reference to its callback method, so I can not use __del__
method also.