# 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. angula学习

    入门 http://www.angularjs.cn/A004 http://www.cnblogs.com/whitewolf/p/angularjs-start.html http://www.n ...

  2. ansible playbook对错误的处理

    Topics Playbooks 中的错误处理 忽略错误的命令 控制对失败的定义 覆写更改结果 Ansible 通常默认会确保检测模块和命令的返回码并且会快速失败 – 专注于一个错误除非你另作打算. ...

  3. jQuery(四):HTML代码操作

    html()可以对HTML代码进行操作,类似于元素JavaScript中的innerHTML. 例如: 示例: <!DOCTYPE html> <html lang="en ...

  4. ssh 断开解决办法

    SSH连接总是隔一段时间没有输入时就断开,解决办法如下: 服务端配置sudo vi /etc/ssh/sshd_configClientAliveInterval 60     #服务端主动向客户端请 ...

  5. Chrome 主页被篡改

    尝试恢复设置.重装chrome.改注册表均无果.最后意外发现解决方法无比简单:删掉桌面上的chrome图标,打开安装文件夹找到chrome.exe,随便改成什么名字.exe,比如baiduwcnm,重 ...

  6. 微信小程序——倒计时功能

    做小程序项目中,需要做一个倒计时功能,如下图: 记录一下实现步骤: 1.考虑到这个功能可能会有多处用到,所以把倒计时的函数写在utils.js里面: const formatNumber = n =& ...

  7. javascript验证键盘keycode

    document.onkeyup = function(event){ var event = event || window.event; alert(event.keyCode); }

  8. Elasticsearch与Solr 选型

    转自:http://blog.csdn.net/jameshadoop/article/details/44905643 搜索引擎选择: Elasticsearch与Solr 搜索引擎选型调研文档 E ...

  9. 启动另一个app

    (一) 通过包名.类名 开发有时需要在一个应用中启动另一个应用,比如Launcher加载所有的已安装的程序的列表,当点击图标时可以启动另一个应用.一般我们知道了另一个应用的包名和MainActivit ...

  10. error: navicat 连接debian系列系统mysql 10038问题解决方案

    还特么有一种可能 阿里云 也有防火墙出口入口规则,这里也有问题