一、使用threadpool模块,支持python2和python3,具体使用方式如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import threadpool
import time
def sayhello(a):
print("hello: "+a)
time.sleep(2)
def main():
name_list = ['liu', 'hai', 'don']
start = time.time()
# 5是线程池中线程的个数
pool = threadpool.ThreadPool(5)
requests = threadpool.makeRequests(sayhello, name_list)
for req in requests:
pool.putRequest(req)
#等待所有任务处理完成,则返回,如果没有处理完,则一直阻塞
pool.wait()
end = time.time()
print("time: "+str(end-start))
start1 = time.time()
for each in name_list:
sayhello(each)
end1 = time.time()
print("time1: "+str(end1-start1))
if __name__ == '__main__':
main()
# 结果(大约节省4秒时间):
# hello: liu
# hello: hai
# hello: don
# time: 2.00300002098
# hello: liu
# hello: hai
# hello: don
# time1: 6.00100016594
# -*- coding: utf-8 -*-
import threadpool
import time
def hello(m, n, o):
print (m, n, o)
print "m = %s, n = %s, o = %s" % (m, n, o)
time.sleep(2)
if __name__ == '__main__':
# 主要找到了两种方法,一种是将参数构造成List进行传入;还有一种是将参数构造成dict进行传入。
# 方法1
lst_vars_1 = ['1', '2', '3']
lst_vars_2 = ['4', '5', '6']
lst_vars_3 = ['7', '8', '9']
func_var = [(lst_vars_1, None), (lst_vars_2, None), (lst_vars_3, None)]
# 方法2
dict_vars_1 = {'m': '1', 'n': '2', 'o': '3'}
dict_vars_2 = {'m': '4', 'n': '5', 'o': '6'}
dict_vars_3 = {'m': '7', 'n': '8', 'o': '9'}
func_var = [(None, dict_vars_1), (None, dict_vars_2), (None, dict_vars_3)]
# 2是线程池中线程的个数
pool = threadpool.ThreadPool(2)
# 通过传入一个参数组来实现多线程,并且它的多线程是有序的,顺序与参数组中的参数顺序保持一致。
requests = threadpool.makeRequests(hello, func_var)
[pool.putRequest(req) for req in requests]
pool.wait()
# pool.poll()
# 结果:
# ('1', '2', '3')
# m = 1, n = 2, o = 3
# ('4', '5', '6')
# m = 4, n = 5, o = 6
# 延迟两秒之后,打印下面的信息
# ('7', '8', '9')
# m = 7, n = 8, o = 9
# 参考:https://www.cnblogs.com/xiaozi/p/6182990.html
# 参考:https://www.cnblogs.com/scios/p/8651758.html
二、使用concurrent.futures模块,这个模块是python3中自带的模块,但是,python2.7以上版本也可以安装使用,具体使用方式如下:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from concurrent.futures import ThreadPoolExecutor
import time
def sayhello(a):
print("hello: "+a)
time.sleep(2)
def main():
seed = ["a", "b", "c"]
start1 = time.time()
for each in seed:
sayhello(each)
end1 = time.time()
print("time1: "+str(end1-start1))
start2 = time.time()
with ThreadPoolExecutor(3) as executor:
for each in seed:
executor.submit(sayhello, each)
end2 = time.time()
print("time2: "+str(end2-start2))
start3 = time.time()
with ThreadPoolExecutor(3) as executor1:
executor1.map(sayhello, seed)
end3 = time.time()
print("time3: "+str(end3-start3))
if __name__ == '__main__':
main()
# 结果:
# hello: a
# hello: b
# hello: c
# time1: 6.00099992752
# hello: a
# hello: b
# hello: c
# time2: 2.02200007439
# hello: a
# hello: b
# hello: c
# time3: 2.01999998093
# 参考:https://www.cnblogs.com/zhang293/p/7954353.html
# 参考:http://c.biancheng.net/view/2627.html