node2:/root/python/20200525#cat t900.py
import asyncio
import aiohttp
import time
async def download_one(url):
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
print('Read {} from {}'.format(resp.content_length, url))
async def download_all(sites):
tasks = [asyncio.create_task(download_one(site)) for site in sites]
await asyncio.gather(*tasks)
def main():
sites = [
'http://192.168.137.3:9000/test111/',
'http://192.168.137.3:9000/test222/',
'http://192.168.137.3:9000/test333/'
]
start_time = time.perf_counter()
asyncio.run(download_all(sites))
end_time = time.perf_counter()
print('Download {} sites in {} seconds'.form
asyncio
最新推荐文章于 2024-06-04 22:18:58 发布
本文详细探讨了Python的asyncio模块,包括其核心概念如协程、事件循环和任务,以及如何利用asyncio实现高效的异步I/O操作。通过实例解析,展示了如何在实际项目中运用asyncio进行并发处理,提升程序性能。
摘要由CSDN通过智能技术生成