• 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

  • 相关阅读:
    winform程序,备份数据库+并压缩+并删除以前的备份
    冒泡排序
    存储过程和SQL语句比较
    简单的firebird插入速度测试
    [收藏转]由于CredSSP加密Oracle修正 导致远程桌面报错处理
    由Handle转换为控件
    字符串格式化
    查询mssql的死锁语句
    读取和修改app.config文件
    json序列化与反序列化
  • 原文地址:https://www.cnblogs.com/uncleyong/p/15871733.html
Copyright © 2020-2023  润新知