#进程池
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time,os
pool=ProcessPoolExecutor(5)
def task(n):
print(n,os.getpid())
time.sleep(2)
return n*n
def call_back(n):
print('call_back>>:',n.result())
if __name__ == '__main__':
for i in range(10):
res=pool.submit(task,i).add_done_callback(call_back)
#线程池
from concurrent.futures import ThreadPoolExecutor
import os,time
t=ThreadPoolExecutor(5)
def task(n):
print(n,os.getpid())
time.sleep(2)
return n
def call_back(n):
print('call_back:',n.result())
if __name__ == '__main__':
for i in range(10):
res=t.submit(task,i).add_done_callback(call_back)