zoukankan      html  css  js  c++  java
  • 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,则需要使用多进程来提高程序运行效率
  • 相关阅读:
    解决iex -S mix报错
    OTP服务器
    多进程
    字符串与二进制
    IphoneX适配正确姿势
    Babel 配置用法解析
    babel版本变化
    你好,babel
    XSS攻击 && CSRF攻击 基础理解
    小程序开发初体验~
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13348353.html
Copyright © 2011-2022 走看看