码迷,mamicode.com
首页 > 其他好文 > 详细

thread/process pool

时间:2021-02-03 10:35:48      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:pre   默认   ==   finish   turn   pool   thread   cal   ini   

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time

def func(i):
print(‘star‘, i)
time.sleep(2)
return f‘over,{i}‘
def cb(res_obj):
print(‘callback=====>‘,res_obj) #callback=====> <Future at 0x21d7eb8 state=finished returned str>
print(‘ccc===>‘,res_obj.result()) #ccc===> over,0
pool=ThreadPoolExecutor(3) #(os.cpu_count() or 1) * 5,默认为当前CPU个数的5倍
#pool=ProcessPoolExecutor() #os.cpu_count() or 1 ,默认为当前CPU的个数

for i in range(1):
res=pool.submit(func, i)
print(res) #<Future at 0x21d7eb8 state=running>
res.add_done_callback(cb)
#pool.submit(func,i).add_done_callback(cb)

thread/process pool

标签:pre   默认   ==   finish   turn   pool   thread   cal   ini   

原文地址:https://www.cnblogs.com/ccqd/p/14361314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!