池模块 -进程池 -线程池

2021-05-19 12:29

阅读:538

标签:spool   tor   exe   func   大数   down   time   sleep   模块   

池模块

线程池concurrent.futures.ThreadPoolExecutor

    from concurrent.futures import ThreadPoolExecutor
    from time import sleep
    
    tpool = ThreadPoolExecutor(max_workers=5)   #设置线程最大数量
    
    def func(i):
        sleep(1)
        print(i)
    
    for i in range(20):
        tpool.submit(func, i)
    tpool.shutdown()    #等待所有子线程运行结束
    print(‘主线程在这里‘)
  • t.result() 获取函数的返回值

进程池concurrent.futures.ProcessPoolExecutor

from concurrent.futures import ProcessPoolExecutor
        from time import sleep
        tpool = ProcessPoolExecutor(max_workers=5)  #这是最大进程数
        
        def func(i):
            sleep(1)
            print(i)
        
        for i in range(20):
            tpool.submit(func, i)
        tpool.shutdown()    #等待所有子进程运行结束
        print(‘主线程在这里‘)
- `t.result()   `   获取函数的返回值

池模块 -进程池 -线程池

标签:spool   tor   exe   func   大数   down   time   sleep   模块   

原文地址:https://www.cnblogs.com/liliudong/p/9742489.html


评论


亲,登录后才可以留言!