Python 从客户端断开连接时发生Aiohttp服务器严重错误
有一个aiohttp服务器。客户机向它发出大量请求。如果客户端和服务器之间的连接突然中断,服务器可能会停止并出现错误:Python 从客户端断开连接时发生Aiohttp服务器严重错误,python,python-3.x,windows,python-asyncio,aiohttp,Python,Python 3.x,Windows,Python Asyncio,Aiohttp,有一个aiohttp服务器。客户机向它发出大量请求。如果客户端和服务器之间的连接突然中断,服务器可能会停止并出现错误: Task exception was never retrieved future: <Task finished name='Task-45565' coro=<IocpProactor.accept.<locals>.accept_coro() done, defined at D:\Python\Python_3_8_2\lib\asyncio\w
Task exception was never retrieved
future: <Task finished name='Task-45565' coro=<IocpProactor.accept.<locals>.accept_coro() done, defined at D:\Python\Python_3_8_2\lib\asyncio\windows_events.py:559> exception=OSError(22, 'The specified network name is no longer available', None, 64, None)>
Traceback (most recent call last):
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 562, in accept_coro
await future
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 808, in _poll
value = callback(transferred, key, ov)
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 551, in finish_accept
ov.getresult()
OSError: [WinError 64] The specified network name is no longer available
Accept failed on a socket
socket: <asyncio.TransportSocket fd=604, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=6, laddr=('0.0.0.0', 80)>
Traceback (most recent call last):
File "D:\Python\Python_3_8_2\lib\asyncio\proactor_events.py", line 801, in loop
conn, addr = f.result()
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 562, in accept_coro
await future
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 808, in _poll
value = callback(transferred, key, ov)
File "D:\Python\Python_3_8_2\lib\asyncio\windows_events.py", line 551, in finish_accept
ov.getresult()
OSError: [WinError 64] The specified network name is no longer available
客户端代码:
import asyncio
import aiohttp
async def do_get(url):
while True:
try:
await asyncio.sleep(1)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
data = await resp.text()
except Exception as ex:
print('Exception:', ex)
async def client():
workers = 5000
tasks = [asyncio.Task(do_get('http://127.0.0.1')) for i in range(workers)]
await asyncio.gather(*tasks)
asyncio.run(client())
要再现错误,需要启动服务器,然后启动客户端,约10秒后终止客户端。服务器中的错误并不总是出现;您可能需要重新启动客户端3-7次。同时,在服务器端,我无法以任何方式捕捉到这种状态,我不知道如何防止它或重新启动服务器
OS:Windows10-Python:3.8.2-aiohttp:3.6.2
import asyncio
import aiohttp
async def do_get(url):
while True:
try:
await asyncio.sleep(1)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
data = await resp.text()
except Exception as ex:
print('Exception:', ex)
async def client():
workers = 5000
tasks = [asyncio.Task(do_get('http://127.0.0.1')) for i in range(workers)]
await asyncio.gather(*tasks)
asyncio.run(client())