Python Multiple Threads

Python 多线程举例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from multiprocessing import *
NUM_THREAD = 10
p = Pool(NUM_THREAD)
start = 0
offset = NUM_THREAD
queries = get_quries("./test.txt") # 获取所有的任务列表
cnt = 0 # 任务的数量
while (start < len(queries)):
end = start + offset
pool_queries = queries[start : end] # 每次取线程个数的任务列表
rslt = p.map(get_qa_rslt, pool_queries) # get_qa_result 是一个函数,调用函数和当前线程的任务列表, rslt保存函数返回值
for i in range(len(pool_queries)):
q = pool_queries[i].strip()
task = rslt[i]
cnt += 1
print task
start = end # 准备下一轮线程
return cnt