python threadpool线程池的使用
ThreadPool
A simple C++11 Thread Pool implementation
项目地址:https://gitcode.com/gh_mirrors/th/ThreadPool
免费下载资源
·
python threadpool
1.不使用线程池
import time
def sayhello(str):
print('hello', str)
time.sleep(2)
name_list = ['wangfei', 'aa', 'bb', 'cc']
start_time = time.time()
for i in range(len(name_list)):
sayhello(name_list[i])
print('%d second' % (time.time()-start_time))
打印结果
hello wangfei
hello aa
hello bb
hello cc
8 second
2.使用线程池
import time
import threadpool
def sayhello(str):
print('hello', str)
time.sleep(2)
name_list = ['wangfei', 'aa', 'bb', 'cc']
start_time = time.time()
# 定义了一个线程池,最多创建10个线程
pool = threadpool.ThreadPool(10)
# 创建要开启多线程的函数,以及函数相关参数和回调函数,其中回调数可以不写,default是none
requests = threadpool.makeRequests(sayhello, name_list)
# 将所有要运行多线程的请求扔进线程池
[pool.putRequest(req) for req in requests]
# 所有的线程完成工作后退出
pool.wait()
print('%d second' % (time.time()-start_time))
打印结果
hello wangfei
hello aa
hello bb
hello cc
2 second
3.使用线程池多值传参的情况
def hello(m, n, o):
print("m = %s, n = %s, o = %s" % (m, n, o))
if __name__ == '__main__':
# # 方法1
# lst_vars_1 = ['1', '2', '3']
# lst_vars_2 = ['4', '5', '6']
# func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
dict_vars_1 = {'m': '1', 'n': '2', 'o': '3'}
dict_vars_2 = {'m': '4', 'n': '5', 'o': '6'}
func_var = [(None, dict_vars_1), (None, dict_vars_2)]
pool = threadpool.ThreadPool(2)
requests = threadpool.makeRequests(hello, func_var)
# [pool.putRequest(req) for req in requests]
print(requests)
for req in requests:
print(req)
pool.putRequest(req)
pool.wait()
打印结果
[<threadpool.WorkRequest object at 0x0000023146648550>, <threadpool.WorkRequest object at 0x0000023146648588>]
<WorkRequest id=2410657645904 args=[] kwargs={‘m’: ‘1’, ‘n’: ‘2’, ‘o’: ‘3’} exception=False>
<WorkRequest id=2410657645960 args=[] kwargs={‘m’: ‘4’, ‘n’: ‘5’, ‘o’: ‘6’} exception=False>
m = 1, n = 2, o = 3
m = 4, n = 5, o = 6
GitHub 加速计划 / th / ThreadPool
7.74 K
2.22 K
下载
A simple C++11 Thread Pool implementation
最近提交(Master分支:2 个月前 )
9a42ec13 - 9 年前
fcc91415 - 9 年前
更多推荐
已为社区贡献2条内容
所有评论(0)