并发编程~~~多线程~~~进程池,线程池

线程池: 一个容器,这个容器限制住你开启线程的数量

以时间换空间

from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor
import os,time,random
def task():
    print(f'{os.getpid()}接客')
    time.sleep(random.randint(1,3))

if __name__ == '__main__':
    p = ProcessPoolExecutor() # 默认不写,进程池里面的进程数与CPU个数相等
    for i in range(20):
        p.submit(task)

    t = ThreadPoolExecutor() # 默认不写,线程数为CPU个数*5 
    for i in range(20):
        t.submit(task)
posted on 2019-11-05 22:49  Lav3nder  阅读(109)  评论(0编辑  收藏  举报