如何解决等待弱引用死亡
这可能不是一个好主意,但我可以让 await
等待 weakref
的“死亡”吗?
特别是我想做这样的事情
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
...
重点就像 queue
可以“关闭”自己而不需要手动通知另一端,因为对它的所有引用都消失了。这会很方便,尤其是在循环中使用时,您可能必须在生产者端手动提高 StopAsyncIteration
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。