|
|
@ -1,4 +1,5 @@
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
from typing import *
|
|
|
|
from typing import *
|
|
|
|
|
|
|
|
|
|
|
|
import aiohttp
|
|
|
|
import aiohttp
|
|
|
@ -16,9 +17,21 @@ http_session: Optional[aiohttp.ClientSession] = None
|
|
|
|
|
|
|
|
|
|
|
|
def init():
|
|
|
|
def init():
|
|
|
|
global http_session
|
|
|
|
global http_session
|
|
|
|
http_session = aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10))
|
|
|
|
http_session = aiohttp.ClientSession(
|
|
|
|
|
|
|
|
response_class=CustomClientResponse,
|
|
|
|
|
|
|
|
timeout=aiohttp.ClientTimeout(total=10),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def shut_down():
|
|
|
|
async def shut_down():
|
|
|
|
if http_session is not None:
|
|
|
|
if http_session is not None:
|
|
|
|
await http_session.close()
|
|
|
|
await http_session.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CustomClientResponse(aiohttp.ClientResponse):
|
|
|
|
|
|
|
|
# 因为aiohttp的BUG,当底层连接断开时,_wait_released可能会抛出CancelledError,导致上层协程结束。这里改个错误类型
|
|
|
|
|
|
|
|
async def _wait_released(self):
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
|
|
return await super()._wait_released()
|
|
|
|
|
|
|
|
except asyncio.CancelledError as e:
|
|
|
|
|
|
|
|
raise aiohttp.ClientConnectionError('Connection released') from e
|
|
|
|