41 lines
836 B
Python
41 lines
836 B
Python
# -*- encoding:utf-8 -*-
|
|
|
|
'''
|
|
@Author : dingjiawen
|
|
@Date : 2023/12/6 16:34
|
|
@Usage : 多任务协程展示协程的优势
|
|
@Desc :
|
|
@参考: https://github.dev/Python3WebSpider/AsyncTest demo11
|
|
|
|
'''
|
|
|
|
import asyncio
|
|
import aiohttp
|
|
import time
|
|
|
|
start = time.time()
|
|
|
|
|
|
async def get(url):
|
|
session = aiohttp.ClientSession()
|
|
response = await session.get(url)
|
|
await response.text()
|
|
await session.close()
|
|
return response
|
|
|
|
|
|
async def request():
|
|
url = 'https://httpbin.org/delay/5'
|
|
print('Waiting for', url)
|
|
response = await get(url)
|
|
print('Get response from', url, 'response', response)
|
|
|
|
|
|
tasks = [asyncio.ensure_future(request()) for _ in range(100)]
|
|
loop = asyncio.get_event_loop()
|
|
loop.run_until_complete(asyncio.wait(tasks))
|
|
|
|
end = time.time()
|
|
print('Cost time:', end - start)
|
|
# Cost time: 7.670234203338623
|