python线程池,进程池学习

经过对比,在没有IO开销的情况下,线程会很快,有IO的情况下,进程会快一些。

import time
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor

def f(x):
    if x % 1000 == 0:
        print(x)


if __name__ == '__main__':
    # 这个分别测试进程池和线程池运行10万次无io 无cpu函数的耗时
    # pool = ProcessPoolExecutor(10)  # 需要42秒
    pool = ThreadPoolExecutor(10)  # 需要0.8秒
    t1 = time.time()
    for i in range(100000):
        pool.submit(f, i)
    pool.shutdown()
    print(time.time() - t1)

原创文章,转载请注明出处:http://124.221.219.47/article/8963/