This might not be a good idea, but can I have await
waiting for "dying" of weakref
?
Especially, I want to do something like this
from asyncio import losing_ref, wait, create_task
from weakref import ref
async def consumer(queue_ref):
while queue_ref() is not None:
await wait(losing_ref(queue_ref), queue_ref().get())
...
async def producer(queue):
for i in range(20):
await queue.put(i)
async def main():
queue = Queue(1)
create_task(sender(queue))
create_task(receiver(ref(queue)))
del queue // make sure `main` is not referencing to it
...
The point is like the queue
can "close" itself without need of manual notification to the other end, since all reference to it is gone. This would be handy especially when used in a loop where you might have to raise StopAsyncIteration
on the producer side manually