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 #线程池, ...
随机推荐
- 微信APP支付服务端开发Java版(一)
一.准备工作 去微信开发者中心下载(扫码支付,里面的大部分代码是可以用的) https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=11 ...
- centos 6.6编译安装git-2.7.0 最新git编译安装
系统环境:centos6.6 1,安装准备 yum -y install gcc zlib-devel openssl-devel perl cpio expat-devel gettext-deve ...
- JS刷新父窗口的几种方式
浮层内嵌iframe及frame集合窗口,刷新父页面的多种方法 <script language=JavaScript> parent.location.reload(); ...
- Java反射机制专题
·Java Reflection Reflection(反射)是被视为动态语言的关键,反射机制允许程序在执行期借助于Reflection API取得任何类的内部信息,并能直接操作任意对象的内部属性及方 ...
- maven 下载 源码和javadoc 命令
摘要:我们在写代码时候,往往是想查看一下源码,看看源码的一些细节内容.一般情况下,在IDE(如eclipse)中近仅仅只需按住ctrl+ 点击对应的方法即可进入对应的源码部分.但是有些时候很多依赖项并 ...
- 可拖拽的ListView
今天研究了一下可拖拽的ListView ,当ListView的数据不能充满整个模拟器的时候,系统自带的listview上拖下拽都不好使,显得很单调, 为了实现上拖下拽时也能实现滚动的效果,这时候就需要 ...
- Spark源码学习1.5——BlockManager.scala
一.BlockResult类 该类用来表示返回的匹配的block及其相关的参数.共有三个参数: data:Iterator [Any]. readMethod: DataReadMethod.Valu ...
- Beta版
Beta版使用说明 各文件介绍:本软件是基于visual studio 2010 平台,使用C#语言开发的windows窗体游戏.该游戏共有七个界面,分别是开始界面,游戏说明界面,模式选择界面,经典模 ...
- C语言程序设计第四次作业
态度决定一切,我依然要说这句话,每次同学们提交的作业,我都会认真评阅,相比实验课而言,可以有更充足的时间来发现问题,很多同学的代码依然会存在一些语法错误或者考虑不周全的现象,我提出了,那么,你认真看了 ...
- iShare.js分享插件
iShare.js是一个小巧的分享插件,纯JS编写,不依赖任何第三方库,使用简便. 为啥写这个插件? 因为在搭建个人blog时(还没有搭建好(¯﹃¯)),对目前国内比较受欢迎的分享插件都不太满意,主要 ...