# 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. Java编程的逻辑 (47) - 堆和PriorityQueue的应用

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  2. linq操作符:聚合操作符

    一.Aggregate操作符 Aggregate操作符对集合值执行自定义聚合运算.来看看Aggregate的定义: public static TSource Aggregate<TSource ...

  3. [技术选型] SSH/SSI框架替代品

    1.Nutz 简介:http://blog.csdn.net/u012373523/article/details/16993859 官网:http://www.nutzam.com/ 2.

  4. bom头解释方法和去掉方法

    什么是bom头? 在utf-8编码文件中BOM在文件头部,占用三个字节,用来标示该文件属于utf-8编码,现在已经有很多软件识别bom头,但是还有些不能识别bom头,比如PHP就不能识别bom头,这也 ...

  5. 【转】Android下使用Properties文件保存程序设置

    原文:http://jerrysun.blog.51cto.com/745955/804789 废话不说,直接上代码.    读取.properties文件中的配置:  String strValue ...

  6. Rethinking the inception architecture for computer vision的 paper 相关知识

    这一篇论文很不错,也很有价值;它重新思考了googLeNet的网络结构--Inception architecture,在此基础上提出了新的改进方法; 文章的一个主导目的就是:充分有效地利用compu ...

  7. Windows 自带的截屏功能

    有时没登陆QQ,又急需截图,系统自带的截图功能就可以派上用场了. 1.按下键盘上的Print Screen 按钮 2.打开系统自带的画图工具,点击粘贴就可以了.

  8. 飞机找不到,流量哪去了?记一次移动WAP网关导致的问题

    这几天随着客户端一个新版本发布,运维发现CDN的流量猛跌: 话说流量就是金钱,流量就是工资.领导很生气,后果很严重.没什么好说的,赶紧查!一开始怀疑服务端有问题,先受伤的总是我们,当然这也是没错的,因 ...

  9. Python_问题收录总结

    python IndentationError: unindent does not match any outer indentation level的问题 用python编个作业,我先用的note ...

  10. 微软office web apps 服务器搭建之在线文档预览

    案例:http://owa.linbsoft.com/op/generate.aspx# 文档地址:http://demo.linbsoft.com/CourseFile/201407/2014070 ...