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'.format(len(sites), end_time - start_time))
if __name__ == '__main__':
main()
node2:/root/python/20200525#python3 t900.py
Read 75784 from http://192.168.137.3:9000/test111/
Read 66390 from http://192.168.137.3:9000/test222/
Read 65672 from http://192.168.137.3:9000/test333/
Download 3 sites in 7.1168726910836995 seconds
node2:/root/python/20200525#
如果是 I/O bound,并且 I/O 操作很慢,需要很多任务 / 线程协同实现,那么使用 Asyncio 更合适。
如果是 I/O bound,但是 I/O 操作很快,只需要有限数量的任务 / 线程,那么使用多线程就可以了。
如果是 CPU bound,则需要使用多进程来提高程序运行效率