concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池。和另外一个第三方threadpool包相比,这个可以非阻塞的运行主进程(前提是自己不主动调用shutdown(Tuue))。
这个包在py3种已经是官方自带了。py2种需要自己安装, pip install  futures

# coding=utf-8
import time
from concurrent.futures import ThreadPoolExecutor
from Logger import Logger
lg=Logger(logname='log4.txt', loglevel=1, logger="concurrent.futures").getlog()
def fun(strx):
time.sleep(2)
print y
print strx def callbackx(rst):
rst.result() threadPoolExecutor=ThreadPoolExecutor(3)
for i in range(20):
futurex=threadPoolExecutor.submit(fun,'hello')
futurex.add_done_callback(callbackx) print 'over'
ThreadPoolExecutor类暴露3个方法是map、submit、shutdown
map传参是一个可迭代的
例如,如果不使用 submit可以这样
threadPoolExecutor.map(fun,['hello']*20)

shutdown方法作用
"""Clean-up the resources associated with the Executor.

It is safe to call this method several times. Otherwise, no other
methods can be called after this one. Args:
wait: If True then shutdown will not return until all running
futures have finished executing and the resources used by the
executor have been reclaimed.
"""
def shutdown(self, wait=True):方法接受两个参数,第二个参数设置成true那么最后一行的print 'over'会在所有hello打印完成后才打印,设置成false会使所有线程没运行完就打印。
关闭后,就不能在使用这个对象submit 或者map其他方法了。
fun函数里面故意写了个print y,y是没定义的,如果不设置回调函数并且捕获日志,是看不到任何错误提示的。

所以要使用回调,并且捕获名为concurrent.futures的log,才能显示出错误。

所以使用这个包时候,一定要设置回调,在回调函数中使用回调结果的result()方法,并且设置捕获日志,否则你函数中一大堆错误,啥都不提示,你还以为代码没毛病呢。


python concurrent.futures包使用,捕获异常的更多相关文章

  1. python concurrent.futures

    python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算.这个论断我们不展开,但是有个概念我们要说明,IO密集型 vs. 计算密集型. IO密集型:读取文件,读取网络套接字频繁. 计算密集 ...

  2. (11)线程池(最新的concurrent.futures包去开启)

    '''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...

  3. python concurrent.futures.Threadpoolexcutor的有界队列和无界队列

    1.默认是无界队列,如果生产任务的速度大大超过消费的速度,则会把生产任务无限添加到无界队列中,这样一来控制不了生产速度,二来是会造成系统内存会被队列中的元素堆积增多而耗尽. 2.改写为有界队列 cla ...

  4. Python标准模块--concurrent.futures

    1 模块简介 concurrent.futures模块是在Python3.2中添加的.根据Python的官方文档,concurrent.futures模块提供给开发者一个执行异步调用的高级接口.con ...

  5. 在python中使用concurrent.futures实现进程池和线程池

    #!/usr/bin/env python # -*- coding: utf-8 -*- import concurrent.futures import time number_list = [1 ...

  6. python简单粗暴多进程之concurrent.futures

    python在前面写过多线程的库threading: python3多线程趣味详解 但是今天发现一个封装得更加简单暴力的多进程库concurrent.futures: # !/usr/bin/pyth ...

  7. python异步并发模块concurrent.futures入门详解

    concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...

  8. Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures

    参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...

  9. python 全栈开发,Day42(Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures)

    昨日内容回顾 线程什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线程是什么关系? 线程是在进程中的 一个执行单位 多进程 本质上开启的这个进程里就有一个线程 多线程 单纯的在当 ...

随机推荐

  1. Microsoft.AspNet.Identity.EntityFramework/IdentityDbContext.cs

    using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; ...

  2. Java设计模式(13)模板模式(Template模式)

    Template模式定义:定义一个操作中算法的骨架,将一些步骤的执行延迟到其子类中. 其实Java的抽象类本来就是Template模式,因此使用很普遍.而且很容易理解和使用,我们直接以示例开始: pu ...

  3. R语言日期的表示和运算(详细总结)

    1.取出当前日期 Sys.Date() [1] "2014-10-29" date()  #注意:这种方法返回的是字符串类型 [1] "Wed Oct 29 20:36: ...

  4. [hadoop读书笔记]前言

    hadoop2.0+主要变化: 全新的MapReduce 2,它建立在一个新的分布式资源管理系统之上,该系统称之为YARN. YARN:分布式资源管理系统

  5. Change Data template dynamically

    1. Attached Property bound to task state. Any change will dynamically set data template.2. Visual St ...

  6. mysql执行带外键的sql文件时出现mysql ERROR 1215 (HY000): Cannot add foreign key constraint的解决

    ERROR 1215 (HY000): Cannot add foreign key constraint 最近在建表时遇到了这个错误,然后找了下找到了解决办法,记录下: 本来是要建两张表: 1 2 ...

  7. MySQL 先按某字段分组,再取每组中前N条记录

    按 gpcode每组 取每组 f4 最大的那条记录: 方法一: select * from calcgsdataflash a where gscode = 'LS_F' and ymd >= ...

  8. QA:Initialization of bean failed; nested exception is java.lang.AbstractMethodError

    Q: <hibernate.version>5.2.10.Final</hibernate.version><dependency> <groupId> ...

  9. unity提高----------射线使用【unity3d 怎样获得当前鼠标点击的对象】

    unity3d 怎样获得当前鼠标点击的对象 最佳答案   var ray = Camera.main.ScreenPointToRay (Input.mousePosition);var hit : ...

  10. Ubuntu+Eclipse+SVN 版本控制配置笔记

    第一步:先更新系统内部软件包缓存(预防出错) #  sudo dpkg --clear-avail #  sudo apt-get update 第二步:安装Eclipse的SVN接口组件“javaH ...