我有一个将在其中包含aiohttp.ClientSession对象的类。
通常在使用时
async with aiohttp.ClientSession() as session:
# some code
由于调用了会话的__aexit__方法,会话将关闭。
我不能使用上下文管理器,因为我想在对象的整个生命周期中保持会话的持久性。
这有效:
import asyncio
import aiohttp
class MyAPI:
def __init__(self):
self.session = aiohttp.ClientSession()
def __del__(self):
# Close connection when this object is destroyed
print('In __del__ now')
asyncio.shield(self.session.__aexit__(None, None, None))
async def main():
api = MyAPI()
asyncio.run(main())
但是,如果在某些地方引发了异常,则在__aexit__方法完成之前将关闭事件循环。 我该如何克服?
stacktrace:
Traceback (most recent call last):
File "/home/ron/.PyCharm2018.3/config/scratches/async.py", line 19, in <module>
asyncio.run(main())
File "/usr/local/lib/python3.7/asyncio/runners.py", line 43, in run
return loop.run_until_complete(main)
File "/usr/local/lib/python3.7/asyncio/base_events.py", line 568, in run_until_complete
return future.result()
File "/home/ron/.PyCharm2018.3/config/scratches/async.py", line 17, in main
raise ValueError
ValueError
In __del__ now
Exception ignored in: <function MyAPI.__del__ at 0x7f49982c0e18>
Traceback (most recent call last):
File "/home/ron/.PyCharm2018.3/config/scratches/async.py", line 11, in __del__
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 765, in shield
File "/usr/local/lib/python3.7/asyncio/tasks.py", line 576, in ensure_future
File "/usr/local/lib/python3.7/asyncio/events.py", line 644, in get_event_loop
RuntimeError: There is no current event loop in thread 'MainThread'.
sys:1: RuntimeWarning: coroutine 'ClientSession.__aexit__' was never awaited
Unclosed client session
client_session: <aiohttp.client.ClientSession object at 0x7f49982c2e10>
答案 0 :(得分:4)
不要使用__del__
钩子来清理异步资源。您根本不能指望它被调用,更不用说控制何时使用它,或者那时异步循环是否仍然可用。您真的想明确地处理这个问题。
要么使API成为异步上下文管理器,要么使用finally
处理程序在出口处显式清理资源。 with
和async with
语句基本上旨在封装传统上由finally
块处理的资源清除。
我将API
实例设置为上下文管理器:
class MyAPI:
def __init__(self):
self.session = aiohttp.ClientSession()
async def __aenter__(self):
return self
async def __aexit__(self, *excinfo):
await self.session.close()
请注意,ClientSession.__aexit__()
真正所做的只是在self.close()
上等待,因此以上内容直接适用于该协程。
然后将其用于您的主循环中:
async def main():
async with MyAPI() as api:
pass
另一种选择是向MyAPI
实例提供您自己的会话对象,并在完成后自行负责关闭该对象:
class MyAPI:
def __init__(self, session):
self.session = session
async def main():
session = aiohttp.ClientSession()
try:
api = MyAPI(session)
# do things with the API
finally:
await session.close()
答案 1 :(得分:0)
正如@Martijn Pieters 所说,您不能强制事件循环等待对象的 __del__
析构函数调用。但是,您仍然可以使用 __del__
析构函数关闭异步资源,方法是首先检查循环是否正在运行,如果不是,则启动一个新循环。例如,asyncio Redis 模块使用这种技术when destructing its Client class。对于您的代码,具体而言,析构函数如下:
import asyncio
import aiohttp
class MyAPI:
def __init__(self):
self.session = aiohttp.ClientSession()
def __del__(self):
# Close connection when this object is destroyed
try:
loop = asyncio.get_event_loop()
if loop.is_running():
loop.create_task(self.session.close())
else:
loop.run_until_complete(self.session.close())
except Exception:
pass