#线程池from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import random
from threading import current_thread
deffun(a,b):print(current_thread().ident,'start',a,b)
time.sleep(random.randint(1,4))print(current_thread().ident,'end')
tb = ThreadPoolExecutor(4)#实例化for i inrange(20):
tb.submit(fun,i,b=i+1)#向任务池中提交任务,submit提交参数(可以按位置传参或者关键字传参)
进程池
import os
import time
import random
from concurrent.futures import ProcessPoolExecutor
deffun(a,b):print(os.getpid(),'start',a,b)
time.sleep(random.randint(1,4))print(os.getpid(),'end')if __name__ =='__main__':
tb = ProcessPoolExecutor(4)#实例化for i inrange(20):
tb.submit(fun,i,b=i+1)#向任务池中提交任务,submit提交参数(可以按位置传参或者关键字传参)
#mapimport os
import time
import random
from concurrent.futures import ProcessPoolExecutor
deffun(a):
b = a+1print(os.getpid(),'start',a,b)
time.sleep(random.randint(1,4))print(os.getpid(),'end')return a*b
if __name__ =='__main__':
tb = ProcessPoolExecutor(4)#实例化
ret = tb.map(fun,range(20))for key in ret:print(key)###回调函数 效率最高from threading import current_thread
import time
import random
from concurrent.futures import ProcessPoolExecutor
deffun(a,b):print(current_thread().ident,'start',a,b)
time.sleep(random.randint(1,4))print(current_thread().ident,'end')return a*b
defprint_fun(ret):#异步阻塞print(ret.result())if __name__ =='__main__':
tb = ProcessPoolExecutor(4)#实例化for i inrange(20):#异步非阻塞
ret = tb.submit(fun,i,b= i+i)
ret.add_done_callback(print_fun)#ret这个任务在执行完毕的瞬间立即触发print_fun函数,并且把任务的返回值#对象传递到print_fun做参数#异步阻塞,回调函数给ret对象绑定一个回调函数,等待ret对应的任务有了结果之后立即调用print_fun这个函数#就可以对结果立即处理,而不用按照顺序接受结果处理结果