forked from ctmakro/opencv_playground
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththready.py
33 lines (24 loc) · 912 Bytes
/
thready.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import threading
def task(f,params):
t = threading.Thread(target=f,args=params)
return t
def fib(x):
return 1 if x<=2 else fib(x-1)+fib(x-2)
import threadpool as tp
pool = tp.ThreadPool(8)
# async map function with multithreading support.
# returned mapresult is a map with integer indices.
def amap(f,plist):
mapresult = {}
def wrapper(idx):
param = plist[idx]
return idx,f(param)
idxlist = range(len(plist))
def taskend(request,result):
idx,res = result
mapresult[idx] = res
reqs = tp.makeRequests(wrapper, idxlist, taskend)
#构建请求,get_title为要运行的函数,data为要多线程执行函数的参数,最后这个print_result是可选的,是对前两个函数运行结果的操作
[pool.putRequest(req) for req in reqs] #多线程一块执行
pool.wait() #线程挂起,直到结束
return mapresult