aiohttp
2020-12-29 18:30
# __author: "ZXYang"
# date: 2020/12/22
"""
[[async]]
async def: 【异步函数】
async with: 【异步上下文】
对象必须符合异步上下文管理器:
__aenter__
__aexit__async for: 【异步迭代】
被迭代对象必须要实现异步迭代协议
__aiter__
__anext__[[await]]
await: 后边只能跟可迭代对象:异步任务,协程, Future,asyncio.sleep()"""
import asyncio
import time
import aiohttp
async def request_url(url):
"""异步请求方法"""
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
res = await response.read()
print(response.status)
async def main():
cos = []
for i in range(10):
cos.append(request_url("http://www.baidu.com"))
await asyncio.gather(*cos)
if __name__ == ‘__main__‘:
s_time = time.time()
asyncio.run(main())
e_time = time.time()
print(e_time - s_time)