我希望创建一个使用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的例子。还想知道为什么第一种方法不起作用?
发布于 2018-11-09 15:19:48
您不能在函数中使用with,并且上下文管理器保持打开状态,不行。with with aiohttp.ClientSession() as session:块一旦使用return退出__aenter__协同线就会退出!
对于具体情况,输入一个aiohttp.ClientSession()上下文管理器self。因此,对于该类型,只需创建实例并将其存储在self.session中,在这里等待self.session.close()就足够了,是的。
嵌套异步上下文管理器的一般模式是等待嵌套异步上下文管理器的__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)发布于 2019-01-11 12:50:39
您可以在外部管理该依赖关系:
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!")https://stackoverflow.com/questions/53228434
复制相似问题