线程池,进程池

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. C# WINFORM判断程序是否运行,且只能运行一个实例(转)

    判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种, 方法1:线程互斥 static class Program { private static System.Threading. ...

  2. JStorm环境搭建

    开始JStorm学习之前需要搭建集群环境,这里演示搭建单机JStorm环境,仅供学习使用,生产环境部署大同小异,但建议参考JStorm社区及相关说明文档. 一.前提 JStorm核心代码均用Java实 ...

  3. 微软收购跨平台移动应用开发商Xamarin

    微软今天宣布收购移动应用跨平台开发商 Xamarin.收购金额未知.Xamarin 提供了通过 C# 开发 iOS.Android 和 Windows 原生移动应用的工具,以及云端应用測试平台 – 全 ...

  4. verilog之四位全加器的编译及仿真(用开源免费的软件——iverilog+GTKWave)

    verilog之四位全加器的编译及仿真(用开源免费的软件——iverilog+GTKWave) 四位全加器的verilog的代码比比皆是,这里上一个比较简单的: /* 4位全加器全加器需要有输入输出, ...

  5. IE6支持兼容min-width、max-width CSS样式属性

    IE6支持兼容min-width.max-width CSS样式属性 让IE6支持max-width.IE6支持min-width样式 我们在写CSS的时候,常常会遇到让一个图片或一个布局不能超出设定 ...

  6. python模块:xlsxwriter和xlrd相结合读取

    python模块简单说明: xlsxwriter:负责写入数据 xlrd:负责读取数据 xlsxwriter 官方文档:http://xlsxwriter.readthedocs.org 本实例是刚写 ...

  7. 基于js白色简洁样式计算器

    今天给大家分享一款白色简洁样式计算器JS代码是一款精美简洁计算器JS代码插件网页特效,软件应用,后台应用JS计算器插件代码免费下载.适用浏览器:360.FireFox.Chrome.Safari.Op ...

  8. UNION types numeric and text cannot be matched

    NULL ::NUMERIC 有时候会遇到这个问题,那是因为几个SQL组合在一起的时候,同一个字段的值,出来了不同类型的时候,这种时候就需要进行转型的处理了.

  9. UCOS2系统内核讲述(四)_创建任务

    Ⅰ.写在前面 学习本文之前可以参看我前面的文章: UCOS2_STM32移植详细过程(汇总文章) UCOS2系统内核讲述(一)_总体描述 UCOS2系统内核讲述(二)_初始化调用函数 UCOS2系统内 ...

  10. PHP——连接数据库初

    <?php //1.生成连接 造连接对象 //$db=new mysqli($dbhost(服务器),$username,$userpass,$dbdatabase); $db = new my ...