线程池,进程池

python的多线程并不是完全鸡肋的存在,得分情况来看。在IO密集型任务下,能提高多倍效率。在CPU密集型任务下,使用多进程也能规避GIL锁。

python3标准库concurrent.futures比原Thread封装更高,多线程concurrent.futures.ThreadPoolExecutor,多进程concurrent.futures.ProcessPoolExecutor

利用concurrent.futures.Future来进行各种便捷的数据交互,包括处理异常,都在result()中再次抛出。

模板

import time
from concurrent import futures
from concurrent.futures import ThreadPoolExecutor def display(args):
print(time.strftime('[%H:%M:%S]', time.localtime()), end=' ')
print(args) def task(n):
"""只是休眠"""
display('begin sleep {}s.'.format(n))
time.sleep(n)
display('ended sleep {}s.'.format(n)) def do_many_task_inorder():
"""多线程
按任务发布顺序依次等待完成
"""
tasks = [5, 4, 3, 2, 1]
with ThreadPoolExecutor(max_workers=3) as executor:
future_list = [executor.submit(task, arg) for arg in tasks] display('非阻塞运行') for future in future_list:
display(future) display('统一结束(有序)') for future in future_list:
display(future.result()) def do_many_task_disorder():
"""多线程执行
先完成先显示
"""
tasks = [5, 4, 3, 2, 1]
with ThreadPoolExecutor(max_workers=3) as executor:
future_list = [executor.submit(task, arg) for arg in tasks] display('非阻塞运行') for future in future_list:
display(future) display('统一结束(无序)') done_iter = futures.as_completed(future_list) # generator for done in done_iter:
display(done) if __name__ == '__main__':
do_many_task_inorder()
do_many_task_disorder()

python线程池ThreadPoolExecutor用法的更多相关文章

  1. python线程池ThreadPoolExecutor(上)(38)

    在前面的文章中我们已经介绍了很多关于python线程相关的知识点,比如 线程互斥锁Lock / 线程事件Event / 线程条件变量Condition 等等,而今天给大家讲解的是 线程池ThreadP ...

  2. python线程池 ThreadPoolExecutor 的用法及实战

    写在前面的话 (https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 文章来源于互联网从Python3.2开始,标准库为我们提供了 concurrent.future ...

  3. python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor

    python中ThreadPoolExecutor(线程池)与ProcessPoolExecutor(进程池)都是concurrent.futures模块下的,主线程(或进程)中可以获取某一个线程(进 ...

  4. Python线程池ThreadPoolExecutor源码分析

    在学习concurrent库时遇到了一些问题,后来搞清楚了,这里记录一下 先看个例子: import time from concurrent.futures import ThreadPoolExe ...

  5. java线程池ThreadPoolExecutor使用简介

    一.简介线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:ThreadPoolExecutor(int corePoolSize, int m ...

  6. 关于线程池ThreadPoolExecutor使用总结

    本文引用自: http://blog.chinaunix.net/uid-20577907-id-3519578.html 一.简介 线程池类为 java.util.concurrent.Thread ...

  7. 线程池ThreadPoolExecutor使用简介

    一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...

  8. 线程池ThreadPoolExecutor使用简介(转)

    一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...

  9. 线程池ThreadPoolExecutor使用

    一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...

随机推荐

  1. TCP/UDP,SOCKET,HTTP,FTP 简析

    (一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...

  2. tp配置+路由+基本操作

    一.打开apache 的配置文件httpd_conf添加以下代码 <VirtualHost *:80>DocumentRoot "D:\wwwroot\thinkphp\publ ...

  3. NYOJ 467 中缀式变后缀式

    做了表达式求值那道题之后做的 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述 人们的日常习惯是把算术表达式写成中缀式,但对于机器来说更"习惯于"后 ...

  4. Spring3的表达式语言

    Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言, 能在运行时构建复杂表达式.存取对象图属性. ...

  5. Java类的实例化的初始化过程

    A a = new A(); new 创建对象过程: 1.类加载     代码验证 2.给对象在内存(堆)中分配空间(给属性赋值): 3.属性赋默认值: byte,short.int,long -&g ...

  6. ngBind {{}} ngBindTemplate

    1.首先我们最常使用的一个绑定表达式的指令是ngBind,比如在一个div标签中我们可以这样使用: <div ng-bind="vm.info"></div> ...

  7. [ADC]Linux ADC驱动

    ADC TI adc user guide: http://processors.wiki.ti.com/index.php/Linux_Core_ADC_Users_Guide 问题: 在tools ...

  8. Java范型之T extends Comparable<? super T>

    在观察Java源码的时候,发现了这么一个写法T extends Comparable<? super T>.不禁纳闷为什么要这么写呢?有什么好处吗,extends和super在这里的作用着 ...

  9. 查看win10的激活信息和版本号

    在命令提示符里输入 cscript /nologo %Windir%\System32\slmgr.vbs /dli&&cscript /nologo %Windir%\System3 ...

  10. 关于JDK环境变量的配置问题

    网上配置JDK环境变量的时候一直说要配置三个环境变量,什么JAVA_HOME,Path,CLASSPATH 其实是说以后如果要修改JDK的版本或者路径,只要更改JAVA_HOME就可以了,Path,C ...