python线程池(threadpool)模块使用笔记
一、安装与简介
pip install threadpool
pool = ThreadPool(poolsize)
requests = makeRequests(some_callable, list_of_args, callback)
[pool.putRequest(req) for req in requests]
pool.wait()
第一行定义了一个线程池,表示最多可以创建poolsize这么多线程;
第二行是调用makeRequests创建了要开启多线程的函数,以及函数相关参数和回调函数,其中回调函数可以不写,default是无,也就是说makeRequests只需要2个参数就可以运行;
第三行用法比较奇怪,是将所有要运行多线程的请求扔进线程池,[pool.putRequest(req) for req in requests]等同于
for req in requests:
pool.putRequest(req)
第四行是等待所有的线程完成工作后退出。
二、代码实例
import time
def sayhello(str):
print "Hello ",str
time.sleep() name_list =['xiaozi','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)

改用线程池代码,花费时间更少,更效率
import time
import threadpool
def sayhello(str):
print "Hello ",str
time.sleep() name_list =['xiaozi','aa','bb','cc']
start_time = time.time()
pool = threadpool.ThreadPool()
requests = threadpool.makeRequests(sayhello, name_list)
[pool.putRequest(req) for req in requests]
pool.wait()
print '%d second'% (time.time()-start_time)

当函数有多个参数的情况,函数调用时第一个解包list,第二个解包dict,所以可以这样:
def hello(m, n, o):
""""""
print "m = %s, n = %s, o = %s"%(m, n, o) if __name__ == '__main__': # 方法1
lst_vars_1 = ['', '', '']
lst_vars_2 = ['', '', '']
func_var = [(lst_vars_1, None), (lst_vars_2, None)]
# 方法2
dict_vars_1 = {'m':'', 'n':'', 'o':''}
dict_vars_2 = {'m':'', 'n':'', 'o':''}
func_var = [(None, dict_vars_1), (None, dict_vars_2)] pool = threadpool.ThreadPool()
requests = threadpool.makeRequests(hello, func_var)
[pool.putRequest(req) for req in requests]
pool.wait()
需要把所传入的参数进行转换,然后带人线程池。
def getuserdic():
username_list=['xiaozi','administrator']
password_list=['root','','abc123!','','password','root']
userlist = [] for username in username_list: user =username.rstrip()
for password in password_list:
pwd = password.rstrip()
userdic ={}
userdic['user']=user
userdic['pwd'] = pwd
tmp=(None,userdic)
userlist.append(tmp)
return userlist
最后
欢迎关注个人微信公众号:Bypass--,每周原创一篇技术干货。

python线程池(threadpool)模块使用笔记的更多相关文章
- Python之路(第四十六篇)多种方法实现python线程池(threadpool模块\multiprocessing.dummy模块\concurrent.futures模块)
一.线程池 很久(python2.6)之前python没有官方的线程池模块,只有第三方的threadpool模块, 之后再python2.6加入了multiprocessing.dummy 作为可以使 ...
- python3 线程池-threadpool模块与concurrent.futures模块
多种方法实现 python 线程池 一. 既然多线程可以缩短程序运行时间,那么,是不是线程数量越多越好呢? 显然,并不是,每一个线程的从生成到消亡也是需要时间和资源的,太多的线程会占用过多的系统资源( ...
- python线程池--threadpool
在爬虫时,有时候解析获得了很多图片或视频地址时,如果一个个下载完成再去下载另一个,这样执行效率太慢了,此时就可用到线程池threadpool,使用基本步骤如下: 1.定于任务函数 2.创建线程池,定义 ...
- 关于python线程池threadpool
#coding=utf-8 import time import threadpool def wait_time(n): print('%d\n' % n) time.sleep(2) #在线程池中 ...
- python线程池(threadpool)
一.安装 pip install threadpool 二.使用介绍 (1)引入threadpool模块 (2)定义线程函数 (3)创建线程 池threadpool.ThreadPool() (4)创 ...
- Python程序中的线程操作(线程池)-concurrent模块
目录 Python程序中的线程操作(线程池)-concurrent模块 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecut ...
- python中多进程multiprocessing、多线程threading、线程池threadpool
浅显点理解:进程就是一个程序,里面的线程就是用来干活的,,,进程大,线程小 一.多线程threading 简单的单线程和多线程运行:一个参数时,后面要加逗号 步骤:for循环,相当于多个线程——t=t ...
- 自定义高级版python线程池
基于简单版创建类对象过多,现自定义高级版python线程池,代码如下 #高级线程池 import queue import threading import time StopEvent = obje ...
- Python 日期时间处理模块学习笔记
来自:标点符的<Python 日期时间处理模块学习笔记> Python的时间处理模块在日常的使用中用的不是非常的多,但是使用的时候基本上都是要查资料,还是有些麻烦的,梳理下,便于以后方便的 ...
- Python线程池任务
#!/usr/bin/env python # -*- coding:utf-8 -*- from concurrent.futures import ThreadPoolExecutor #线程池, ...
随机推荐
- Microsoft Word 段前距设置和页眉设置
在毕设论文整理中,段前距的设置和页眉的设置不起作用,这着实让我脑袋疼.网上搜索了几种方式,搭配一下还是起了作用. 一. 段前距设置 1. 各级标题在首页,段前距设置不起作用. 选择要段前分页的段落,右 ...
- Java线程池使用说明
Java线程池使用说明 转自:http://blog.csdn.net/sd0902/article/details/8395677 一简介 线程的使用在java中占有极其重要的地位,在jdk1.4极 ...
- android学习之RadioButton和CheckBox
移通152 余继彪 RadioBuuton是一个单选按钮,CheckBox是一个复选按钮 . RadioButton的使用 ,首先要将RadioButton放在RadioGroup中,RadioGro ...
- 字母序列递增,即A+1=B,B+2=D,ASCII
实际遇到的问题是 单号15001订单的15001-A自动生成15001-B,15001-C.... //说明:以15001-A为基准生成15001-B string maxno ="1500 ...
- Linux编程 ---- dup函数
dup,dup2,dup3函数 顾名思义,dup及duplicate的简写,也就是复制的意思.而事实上这几个函数的功能也确实是复制文件描述符.那为什么要复制文件描述符呢?呵呵,我认为是程序 ...
- EF升级6.0数据库链接不上问题
昨天搞了个mvc4 先从net4.0 升级4.5后 数据库连接不上了, 然后升级ef未最新的6.1 居然还报错 不到方法:“System.Data.Objects.ObjectContext S ...
- Integer Break(Difficulty: Easy)
题目: Given a positive integer n, break it into the sum of at least two positive integers and maximize ...
- pb自动注册ole控件
方法一: 1.手工注册OCX控件 将该控件随程序一起发布,然后,将此文件拷到windows\system,或者直接放在本运行目录,然后执行dos命令,run( "regsvr32 *. ...
- cookie 保存导航菜单的展开状态
菜单展开状态保存,最开始是用session来存的,用session存没点击一次菜单就会多次访问后台页面,影响页面加载速度,后来改用js的cookie来存:代码如下 //获取cookie ...
- 移动安全初探:窃取微信聊天记录、Hacking Android with Metasploit
在这篇文章中我们将讨论如何获取安卓.苹果设备中的微信聊天记录,并演示如何利用后门通过Metasploit对安卓设备进行控制.文章比较基础.可动手性强,有设备的童鞋不妨边阅读文章边操作,希望能激发大家对 ...