python打造线程池
# coding=utf-8
import threading
import Queue
import time
import traceback class ThreadPoolExecutor(object):
def __init__(self, max_works):
self._q = Queue.Queue()
self.max_works=max_works
self.started=0 def worker(self):
while True:
(fn, args) = self._q.get()
try:
fn(*args)
except Exception, e:
print '线程池执行错误,item是:',item, '错误原因是:',e,traceback.format_exc()
finally:
pass
self._q.task_done() def submit(self, fn, *args):
item = (fn,args)
self._q.put(item)
self.start_work() def start_work(self):
if self.started==0:
for i in range(self.max_works):
t=threading.Thread(target=self.worker)
t.setDaemon(True) ###利用daemon的特性:父线程退出时子线程就自动退出。
t.start()
self.started=1 def wait_all_finish(self):
self._q.join()
测试
#coding=utf8
import threading
from xccfb import ThreadPoolExecutor
import time if __name__=="__main__":
tlock=threading.Lock()
def fun(strx):
with tlock:
print time.strftime('%H:%M:%S'),strx
time.sleep(2) threadPoolExecutor=ThreadPoolExecutor(3)
for i in range(10):
threadPoolExecutor.submit(fun, 'hello')
threadPoolExecutor.submit(fun, 'hi')
threadPoolExecutor.wait_all_finish() ###注释掉就可以先print over
print time.strftime('%H:%M:%S'), 'over'

使用wait_all_finish()的queue.join()方法阻塞主线程,当队列中有任务还要执行时候不往下执行。不想阻塞就不要写这句。
python打造线程池的更多相关文章
- Python的线程池实现
# -*- coding: utf-8 -*- #Python的线程池实现 import Queue import threading import sys import time import ur ...
- Python之路【第八篇】python实现线程池
线程池概念 什么是线程池?诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...
- Python之线程池
版本一: #!/usr/bin/env python # -*- coding:utf-8 -*- import Queue import threading class ThreadPool(obj ...
- python自定义线程池
关于python的多线程,由与GIL的存在被广大群主所诟病,说python的多线程不是真正的多线程.但多线程处理IO密集的任务效率还是可以杠杠的. 我实现的这个线程池其实是根据银角的思路来实现的. 主 ...
- Python的线程池
#!/usr/bin/env python # -*- coding: utf-8 -*- """ concurrent 用于线程池和进程池编程而且更加容易,在Pytho ...
- [python] ThreadPoolExecutor线程池 python 线程池
初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...
- 《Python》线程池、携程
一.线程池(concurrent.futures模块) #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 P ...
- [python] ThreadPoolExecutor线程池
初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...
- python实现线程池
线程池 简单线程池 import queue import threading import time class ThreadPool(object): #创建线程池类 def __init__(s ...
随机推荐
- jstorm之于storm
关于流处理框架,在先前的文章汇总已经介绍过Strom,今天学习的是来自阿里的的流处理框架JStorm.简单的概述Storm就是:JStorm 比Storm更稳定,更强大,更快,Storm上跑的程序,一 ...
- 莫衷一是——i+++j 该怎样计算?
这是一个有趣的计算, 3 个加号相连.那么,究竟是怎样结合的呢?是依照: i + (++j)来运算,还是依照(i++) + j 来运算呢? 这个问题在相似于 C / C++中讨论是没有多大意义的,由于 ...
- 【LINUX】——FreeBSD中的一些常规配置
一:在为终端的目录添加颜色: 在 ~/.cshrc 文件中添加以下两行: setenv CLICOLOR 1 setenv LSCOLORS Gxfxaxdxcxegedabagacad CLICOL ...
- HashTable和HashMap的区别详解
一.HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长. HashMap是非线程安全的, ...
- 【转】jmeter 如何将上一个请求的结果作为下一个请求的参数——使用正则提取器
1.简介 Apache JMeter是Apache组织开发的基于Java的压力测试工具.用于对软件做压力测试,它最初被设计用于Web应用测试但后来扩展到其他测试领域. 它可以用于测试静态和动态资源例如 ...
- What is systemvolumeinformation? delete it?
System Volume Information完全可以删除 许多人为了自己的电脑上的System Volume Information不知道而苦恼..我再此给大家介绍一下希望能给你点帮助.. Sy ...
- (笔记)Linux下C语言实现静态IP地址,掩码,网关的设置
#include <sys/ioctl.h> #include <sys/types.h> #include <sys/socket.h> #include < ...
- CentOS 6.8 安装 RabbitMQ
放上来做个备份. 1.下载RabbitMQ http://www.rabbitmq.com/download.html 选择 RHEL/CentOS 6.x 下载即可. 或者 http://www.r ...
- 第三百五十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行、scrapy-splash、splinter
第三百五十二节,Python分布式爬虫打造搜索引擎Scrapy精讲—chrome谷歌浏览器无界面运行.scrapy-splash. splinter 1.chrome谷歌浏览器无界面运行 chrome ...
- Spring系列(一):Spring的基本概念及其核心
一.Spring是什么 Spring是一种多层的J2EE应用程序框架,其核心就是提供一种新的机制管理业务对象及其依赖关系. 二.为什么要使用Spring 1. 降低组件之间的耦合度,实现软件各层之间的 ...