遇一山,过一山,处处有风景;只要勇敢向前,一路尽是繁花盛开。 | (点击查看→)【测试干货】python/java自动化、持续集成、性能、测开、简历、笔试面试等

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

posted @ 2022-02-08 18:11  全栈测试笔记  阅读(295)  评论(1编辑  收藏  举报
浏览器标题切换
浏览器标题切换end