Jos*_*Fox 5 python-3.x python-asyncio aiohttp
"Cannot write to closing transport"我收到了从 引发的异常aiohttp.http_writer.StreamWriter#_write,但仅在一小部分情况下。
相关片段。
session: aiohttp.ClientSession
async with session.get(url, timeout=60) as response:
txt = await response.text()
response.close()
return txt
Run Code Online (Sandbox Code Playgroud)
到底是怎么回事?我不认为服务器大小正在关闭套接字。
答案:我们应该为每个请求创建一个新会话。也不需要response()显式关闭,因为上下文管理器会处理它。
async with aiohttp.ClientSession().get(url, timeout=60) as response:
txt = await response.text()
return txt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12213 次 |
| 最近记录: |