线程池,进程池

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. MySQL-Transfer2.3发布

    Transfer 2.3发布,下载地址 此版本除了升级based版本外 *优化了无索引表的同步性能 *优化了slave模式下超大事务内存消耗问题 *Transfer模式相关的功能改动较多 *修复tra ...

  2. C++ 基类指针和子类指针相互赋值

    首先,给出基类animal和子类fish [cpp] view plaincopy //======================================================== ...

  3. CYQ聊天遇到的问题

    action.Data["yj_id"].Value  用action.Get<int>("yj_id"); 这种写法安全 如果是代码里怎么判断,a ...

  4. Spring学习10-SpringMVC原理及核心组件1

    一.SpringMVC原理     请求到来时,第一个接受这个请求的前端控制器叫DispatcherServlet(这个需要在web.xml中配置), 后端控制器叫Controller.负责处理请求U ...

  5. tornado部署

    1.为什么要运行多个tornado实例同步请求时,在应用处理过程中(如数据库查询,磁盘访问),服务器进程不能接受新请求.所以需要运行多个服务器进程实例.异步请求时,在应用处理时,服务器进程是非阻塞的, ...

  6. 【C语言】21-结构体

    C语言的核心部分都说得七七八八了,相信大家已经对C语言的基本数据类型(char\int\float).数组.指针都很熟悉了,今天来学习C语言中另外一种数据类型:结构体.在iOS开发中,结构体是经常用到 ...

  7. 远程登录linux上的mysql数据库

    UPDATE user SET `Host` = '%' WHERE `User` = 'root' LIMIT 1; 讲root账户,的HOST设置为%,允许所有公网IP访问. flush priv ...

  8. JAVA第一个窗体小程序

    import java.awt.*;public class Day1015_Frame{    public static void main(String[] args)    {         ...

  9. 关于锚点(anchorPoint)的一些理解

    默认来说,锚点位于图层的中点.对锚点一直不太了解是怎么移动的,有时候搞明白过不久碰到了又是懵逼,这次专门记录下来自己的一些理解,有不对的也请指教一下.就用下张图来简单的说明,每个视图的左上角锚点位置为 ...

  10. 基于HTML5坦克大战游戏简化版

    之前我们有分享过不少经典的HTML5游戏,有些还是很有意思的,比如HTML5版切水果游戏和HTML5中国象棋游戏.今天要分享的是一款简化版的HTML5坦克大战游戏,方向键控制坦克的行进方向,空格键发射 ...