33 lines
797 B
Python
33 lines
797 B
Python
#-*- encoding:utf-8 -*-
|
|
|
|
'''
|
|
@Author : dingjiawen
|
|
@Date : 2023/12/6 16:34
|
|
@Usage : 多任务协程展示协程的优势
|
|
@Desc :
|
|
@参考: https://github.dev/Python3WebSpider/AsyncTest demo8_1和demo9_1 demo10
|
|
|
|
'''
|
|
|
|
|
|
import asyncio
|
|
import requests
|
|
import time
|
|
|
|
start = time.time()
|
|
|
|
|
|
# 单个执行每个都至少要5秒
|
|
async def request():
|
|
url = 'https://httpbin.org/delay/5'
|
|
print('Waiting for', url)
|
|
# 这里无论是加await还是不加await都无法实现真正意义上的异步 需要使用aiohttp
|
|
response = requests.get(url)
|
|
print('Get response from', url, 'response', response)
|
|
|
|
tasks = [asyncio.ensure_future(request()) for _ in range(10)]
|
|
loop = asyncio.get_event_loop()
|
|
loop.run_until_complete(asyncio.wait(tasks))
|
|
|
|
end = time.time()
|
|
print('Cost time:', end - start) |