https://www.cnblogs.com/shenh/p/9090586.html
import aiohttp
import asyncio
from aiohttp.client_exceptions import ClientOSError
from typing import *
import logging
logger = logging.getLogger(__name__)
class RequestBuilder(object):
def __init__(self, method: str, url: str, **kwargs: Any):
self.method = method
self.url = url
self.__dict__.update(kwargs)
async def fetch(func, **kwargs: Any) -> Any:
async with aiohttp.ClientSession() as session: # 协程嵌套,只需要处理最外层协程即可fetch_async
try:
async with session.request(**kwargs) as resp:
await func(resp)
# return await resp.read()
except (ClientOSError, asyncio.TimeoutError) as e:
logger.error(e)
async def bound_future(func, *request_builders: RequestBuilder) -> Any:
async with asyncio.Semaphore(1000):
return await asyncio.gather(
*[asyncio.ensure_future(fetch(func, **builder.__dict__)) for builder in request_builders])
def async_request(func) -> Any:
def decorator(event_loop: Any, *request_builders: RequestBuilder):
return event_loop.run_until_complete(bound_future(func, *request_builders))
return decorator
if __name__ == '__main__':
event_loop = asyncio.get_event_loop()
@async_request
async def test(resp):
c = await resp.read()
print(c)
url_list = []
test(event_loop, *[RequestBuilder('GET', url) for url in url_list])
event_loop.close()