(18)ProcessPoolExecutor进程池

# 新版本的进程池 ProcessPoolExecutor

# 实例化进程池 ProcessPoolExcutor(cpu_count)

# 异步提交任务 submit / map

# 阻塞直到任务完成 shutdown

# 获取子进程的返回值 result

# 使用回调函数 add_done_callback

基本用法和线程池语法一样

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import time
def func(i):
    print("Process", i)
    time.sleep(1)
    print("Process is end ")
if __name__ == "__main__":
    # ProcessPoolExecutor <==> Pool
    p = ProcessPoolExecutor(5)
    # submit <==> apply_async
    p.submit(func, 1)
    # shutdown <==> close + join
    p.shutdown()
    print("主线程")
View Code

执行结果:

Process 1
Process is end 
主线程
View Code

 

 

 

转载于:https://www.cnblogs.com/lyj910313/p/10787391.html

你可能感兴趣的:((18)ProcessPoolExecutor进程池)