# 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打造线程池的更多相关文章

  1. Python的线程池实现

    # -*- coding: utf-8 -*- #Python的线程池实现 import Queue import threading import sys import time import ur ...

  2. Python之路【第八篇】python实现线程池

    线程池概念 什么是线程池?诸如web服务器.数据库服务器.文件服务器和邮件服务器等许多服务器应用都面向处理来自某些远程来源的大量短小的任务.构建服务器应用程序的一个过于简单的模型是:每当一个请求到达就 ...

  3. Python之线程池

    版本一: #!/usr/bin/env python # -*- coding:utf-8 -*- import Queue import threading class ThreadPool(obj ...

  4. python自定义线程池

    关于python的多线程,由与GIL的存在被广大群主所诟病,说python的多线程不是真正的多线程.但多线程处理IO密集的任务效率还是可以杠杠的. 我实现的这个线程池其实是根据银角的思路来实现的. 主 ...

  5. Python的线程池

    #!/usr/bin/env python # -*- coding: utf-8 -*- """ concurrent 用于线程池和进程池编程而且更加容易,在Pytho ...

  6. [python] ThreadPoolExecutor线程池 python 线程池

    初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...

  7. 《Python》线程池、携程

    一.线程池(concurrent.futures模块) #1 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 P ...

  8. [python] ThreadPoolExecutor线程池

    初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...

  9. python实现线程池

    线程池 简单线程池 import queue import threading import time class ThreadPool(object): #创建线程池类 def __init__(s ...

随机推荐

  1. tomcat出现的PermGen Space问题<转>

    最近做项目碰到了让我纠结的问题,tomcat服务器运行一段时间,总是会自动报异常:java.lang.OutOfmemoryError: PermGen Space 的错误,导致项目无法正常运行. 出 ...

  2. IDEA成功注册方法

    IDEA成功注册方法 1. 到网站 http://idea.lanyus.com/ 获取注册码. 2.填入下面的license server: http://intellij.mandroid.cn/ ...

  3. C# 不借助第三个变量实现两整数交换

    c#中实现两个int变量的数值互换,在借助第三个变量做临时载体的情况下,非常简单. ; ; int c ; c = a; a = b; b = c; 如果不借助第三方变量,有几种实现的方法: 1.异或 ...

  4. android开发(33) 让 actionbar 透明2

    让 actionbar 的背景 透明 我需要一个 透明的actionbar ,找到如下方法实现: 1. 首先,设置ActionBar 浮动到主界面上来. 2. 然后,设置ActionBar的背景色,透 ...

  5. I/O模型(同步、非同步、阻塞、非阻塞)总结

    I/O:同步(synchronous).异步(asynchronous).阻塞(blocking).非阻塞(nonblocking) 1.I/O内部机制 出于安全考虑,用户程序(用户态)是没办法直接操 ...

  6. 【转】接口测试Session/Cookie笔记(二)

    Windows系统运行计算器命令:calc python显示上一步操作命令:Alt+p python显示上一步操作结果:_(英文下划线) Session是存放在服务器的键值对 ,用于保存客户端的某个特 ...

  7. Reusable async validation for WPF with Prism 5

    WPF has supported validation since the first release in .NET 3.0. That support is built into the bin ...

  8. A SIMPLE LIBRARY TO BUILD A DEEP ZOOM IMAGE

    My current project requires a lot of work with Deep Zoom images. We recently received some very high ...

  9. QIODevice (Qt中所有 I/O devices 的基类,QFile,QBuffer,QTcpSocket等)

    QIODevice是所有Qt I/O设备的基类,它提供了对支持块读写设备(例如QFile,QBuffer,QTcpSocket)的通用实现和抽象接口.QIODevice是一种抽象,不能被实例化,但是, ...

  10. java.lang.ClassCastException: java.lang.Long cannot be cast to java.lang.Integer

    hibernate查询结果条数集 原写法: Integer count = (Integer )session.createQuery(hql).uniqueResult(); 报错:java.lan ...