标签:协议 异步 异步请求 run for request wait pen com
# __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)
标签:协议 异步 异步请求 run for request wait pen com
原文地址:https://www.cnblogs.com/zxy01/p/14176146.html