• asyncio


    
    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,则需要使用多进程来提高程序运行效率
  • 相关阅读:
    js数组和数组去重的几种简单的方法
    nodejs项目的model操作mongo
    canvas画布
    bson
    神奇的东西
    sql与nosql
    mong大牛的blog
    mongo 增删改查
    Mongo配置基础
    session放数据库里解决丢失的问题
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348353.html
Copyright © 2020-2023  润新知