【发布时间】:2020-03-26 15:08:41
【问题描述】:
假设一个类需要一个异步协程来清理:
import asyncio
class AsyncClient:
async def do_something(self):
print ('Doing something')
await asyncio.sleep(1)
print ('Something done')
async def cleanup(self):
print ('Starting cleanup')
await asyncio.sleep(1)
print ('Cleanup in progress 1/3')
await asyncio.sleep(1)
print ('Cleanup in progress 2/3')
await asyncio.sleep(1)
print ('Cleanup in progress 3/3')
def __del__(self):
**CODE_HERE_SHOULD_CALL_CLEANUP**
dunder方法中不允许使用await。
我应该在__del__ 方法中添加什么来允许在这两种情况下进行清理:
client = AsyncClient()
async def main():
await client.do_something()
asyncio.run(main())
或
async def main():
client = AsyncClient()
await client.do_something()
asyncio.run(main())
我尝试执行以下操作,这适用于第一种情况,但不适用于第二种情况(如果循环不再存在,我会重新创建一个循环):
def __del__(self):
print ('__del__()')
try:
loop = asyncio.get_event_loop()
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
asyncio.run(self.cleanup())
return
cleanup_task = loop.create_task(self.cleanup())
【问题讨论】:
-
您可能希望使用上下文管理器,而不是依赖
__del__来调用。
标签: python python-asyncio python-3.8