• I/O密集型任务下,单线程、多进程、多线程、协程


    单线程

    import time
    
    def html(url):
        time.sleep(0.1)
        print(url)
    
    if __name__=='__main__':
        start_time = time.time()
    
        for i in range(1,1001):
            i = 'http://www.cnblogs.com/uncleyong/%s'%i
            html(i)
    
        print('---------------')
        print('ok')
        stop_time = time.time()
        print(stop_time - start_time)
    

    ---------------
    ok
    100.0597231388092

     

    多进程

    import multiprocessing
    import time
    
    def html(url):
        time.sleep(0.1)
        print(url)
    
    
    if __name__=='__main__':
        start_time = time.time()
        p_list = []
        for i in range(1,1001):
            i = 'http://www.cnblogs.com/uncleyong/%s'%i
            p = multiprocessing.Process(target=html, args=(i,))
            p_list.append(p)
            p.start()
    
        for p in p_list:
            p.join()
    
        print('---------------')
        print('ok')
        stop_time = time.time()
        print(stop_time - start_time)
    

    ---------------

    ok
    80.07257986068726

    多线程

    import threading
    import time
    
    def html(url):
        time.sleep(0.1)
        print(url)
    
    
    if __name__=='__main__':
        start_time = time.time()
        t_list = []
        for i in range(1,1001):
            i = 'http://www.cnblogs.com/uncleyong/%s'%i
            t = threading.Thread(target=html, args=(i,))
            t_list.append(t)
            t.start()
    
        for t in t_list:
            t.join()
    
        print('---------------')
        print('ok')
        stop_time = time.time()
        print(stop_time - start_time)
    

    ---------------
    ok
    0.23501324653625488

    协程

    from gevent import monkey;monkey.patch_all()
    import gevent
    import time
    
    def html(url):
        time.sleep(0.1)
        print(url)
    
    if __name__=='__main__':
        start_time = time.time()
        g_list = []
        for i in range(1,1001):
            i = 'http://www.cnblogs.com/uncleyong/%s'%i
            g = gevent.spawn(html, i)
            g_list.append(g)
    
        for g in g_list:
            g.join()
    
        print('---------------')
        print('ok')
        stop_time = time.time()
        print(stop_time - start_time)
    

    ---------------
    ok
    0.14500832557678223

    运行速度:协程 > 多线程 > 多进程 > 单线程

    bak:https://www.cnblogs.com/uncleyong/p/8503151.html

    原文:https://www.cnblogs.com/uncleyong/p/15871733.html

  • 相关阅读:
    webpack4 css modules
    CSS多种方式实现底部对齐
    十大Web开发趋势
    CSS float相关介绍
    Web 堆栈选择指南:JAMStack vs MEAN vs LAMP
    a标签调用js函数写法总结
    如何给网页划分结构?
    九大高效的前端测试工具与框架
    移动端手机验证码四格、六格的input实现
    在modelarts上部署backend为TensorFlow的keras模型
  • 原文地址:https://www.cnblogs.com/uncleyong/p/15871733.html
Copyright © 2020-2023  润新知