我想创建一个使用aiohttp进行API调用的调度程序类。我尝试过:
import asyncio
import aiohttp
class MySession:
def __init__(self):
self.session = None
async def __aenter__(self):
async with aiohttp.ClientSession() as session:
self.session = session
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
await self.session.close()
async def method1():
async with MySession() as s:
async with s.session.get("https://www.google.com") as resp:
if resp.status == 200:
print("successful call!")
loop = asyncio.get_event_loop()
loop.run_until_complete(method1())
loop.close()
但这只会导致错误:RuntimeError: Session is closed
。 __aenter__
函数的第二种方法:
async def __aenter__(self):
self.session = aiohttp.ClientSession()
return self
效果很好。这是一个好结构吗?它不遵循如何使用aiohttp的示例。还想知道为什么第一种方法不起作用?
答案 0 :(得分:4)
您不能在函数内部使用with
并使上下文管理器保持打开状态,否。一旦您使用with with aiohttp.ClientSession() as session:
退出return
协程,__aenter__
块就会退出!
在特定情况下,输入aiohttp.ClientSession()
上下文管理器does nothing but return self
。因此,对于那种类型,只需创建实例并将其存储在self.session
中,然后等待self.session.close()
就可以了。
嵌套异步上下文管理器的 general 模式将等待您自己的此类方法中嵌套异步上下文管理器的__aenter__
和__aexit__
方法(并可能通过以及异常信息):
class MySession:
def __init__(self):
self.session = None
async def __aenter__(self):
self.session = aiohttp.ClientSession()
await self.session.__aenter__()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
return await self.session.__aexit__(exc_type, exc_val, exc_tb)
从技术上讲,在进入嵌套上下文管理器之前,您首先应确保有一个实际的__aexit__
属性:
class MySession:
def __init__(self):
self.session = None
self._session_aexit = None
async def __aenter__(self):
self.session = aiohttp.ClientSession()
self._session_aexit = type(self.session).__aexit__
await self.session.__aenter__()
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
if self.session:
return await self._session_aexit.__aexit__(
self.session, exc_type, exc_val, exc_tb)
答案 1 :(得分:0)
您可以从外部管理该依赖关系:
import asyncio
import aiohttp
class MySession:
def __init__(self, client_session):
self.session = client_session
async def __aenter__(self):
return self
async def __aexit__(self, exc_type, exc_val, exc_tb):
pass
async def method1():
async with aiohttp.ClientSession() as client_session:
async with MySession(client_session) as s:
async with s.session.get("https://www.google.com") as resp:
if resp.status == 200:
print("successful call!")
asyncio.run(method1())
当async with
链变得太荒谬时,您可以使用AsyncExitStack
:
from contextlib import AsyncExitStack
async def method1():
async with AsyncExitStack() as stack:
client_session = await stack.enter_async_context(aiohttp.ClientSession())
s = await stack.enter_async_context(MySession(client_session))
async with s.session.get("https://www.google.com") as resp:
if resp.status == 200:
print("successful call!")