【python】threadpool线程池模块基本用法

安装:
pip install threadpool

0x01 代码剖析

创建线程池,线程数为10:

pool = threadpool.ThreadPool(10) 

创建线程请求,包涵调用的函数、参数和回调函数:

requests = threadpool.makeRequests(func, args_list, call_back)
# 源代码
# `args_list`` should be either a 2-item tuple of the list of positional arguments and a dictionary of keyword arguments or a single, non-tuple argument.

args_list必须是包含2个元素的元组,第一个是list,第二个是dict,如果线程函数需要多个参数,需要拼接list或者dict。

    # 方法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.putRequest(req) for req in requests]

# 等同于
for req in requests:  
    pool.putRequest(req)

等待所有的线程完成工作后退出:

pool.wait()

示例:


#!/usr/bin/env python
# coding:utf-8

import time
import random
import threadpool

HEHE = dict()

def sayhello(name, v):
    global HEHE
    if HEHE.has_key(name):
        HEHE[name] = HEHE[name] + '+' + v
    else:
        HEHE[name] = v
    #time.sleep(2)


#name_list = [(['caoshuai', '1'], None), (['yangliu', '2'], None),(['caoshuai', '3'], None),(['ss', '10'], None),(['wwwwww', '12'], None),]
name_list = [(['caoshuai','1'],None),(['caoshuai','2'],None),(['a','3'],None),(['ss','10'],None),(['wwwwww','12'],None),(['m','12'],None),(['n','12'],None),(['b','12'],None),(['v','12'],None),(['x','12'],None),(['z','12'],None),]

#name_list = [1, -5, 6, -4]

start_time = time.time()

pool_t = threadpool.ThreadPool(4)

requestss = threadpool.makeRequests(sayhello, name_list)

[pool_t.putRequest(req) for req in requestss]

pool_t.wait()
print HEHE
print "%s second" % (time.time()-start_time)

while True:
    time.sleep(1)



作者:lndyzwdxhs
链接:https://www.jianshu.com/p/adf926412de0
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值