python线程池ThreadPoolExecutor用法
线程池,进程池
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用法的更多相关文章
- python线程池ThreadPoolExecutor(上)(38)
在前面的文章中我们已经介绍了很多关于python线程相关的知识点,比如 线程互斥锁Lock / 线程事件Event / 线程条件变量Condition 等等,而今天给大家讲解的是 线程池ThreadP ...
- python线程池 ThreadPoolExecutor 的用法及实战
写在前面的话 (https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 文章来源于互联网从Python3.2开始,标准库为我们提供了 concurrent.future ...
- python线程池ThreadPoolExecutor与进程池ProcessPoolExecutor
python中ThreadPoolExecutor(线程池)与ProcessPoolExecutor(进程池)都是concurrent.futures模块下的,主线程(或进程)中可以获取某一个线程(进 ...
- Python线程池ThreadPoolExecutor源码分析
在学习concurrent库时遇到了一些问题,后来搞清楚了,这里记录一下 先看个例子: import time from concurrent.futures import ThreadPoolExe ...
- java线程池ThreadPoolExecutor使用简介
一.简介线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:ThreadPoolExecutor(int corePoolSize, int m ...
- 关于线程池ThreadPoolExecutor使用总结
本文引用自: http://blog.chinaunix.net/uid-20577907-id-3519578.html 一.简介 线程池类为 java.util.concurrent.Thread ...
- 线程池ThreadPoolExecutor使用简介
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- 线程池ThreadPoolExecutor使用简介(转)
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- 线程池ThreadPoolExecutor使用
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
随机推荐
- 详解php的安装模式---CGI,FASTCGI,php-fpm,mod_php,mod_cgi,mod_fcgid
1. CGI CGI是通用网关接口,HTTP服务器使用这样的接口程序来和“其他程序”(比如PHP的解释器程序)通讯,这个“其他程序”可以使用任何计算机语言来编写,它通过CGI这个接口从HTTP服务器取 ...
- [svc]nfs客户端报错解决Stale file handle
NFS故障: 问题背景: 客户端挂载是好的.服务端磁盘满了,重新给挂了一快.客户端df -h 发现nfs挂载消失. 查看目录客户端报错:Stale file handle 现象如下: [root@n1 ...
- php chr() ord()中文截取乱码问题解决方法
今天看到chr() ord()中文截取乱码问题这个例子,觉得相当的不错,拿出来和大家分享下,有兴趣的朋友可以去试下,看看怎么样. 代码如下: <?php $lenth = ; $str = &q ...
- 每日英语:Investing the Downward Dog Way? Adviser Suggests Deep Breaths
When the Dow Jones Industrial Average hit a new record this past March, Brent Kessel awoke at 3:30 a ...
- oracle查询数据库最大连接数等信息
.当前的数据库连接数 select count(*) from v$process where program='ORACLE.EXE(SHAD)'; .数据库允许的最大连接数 select valu ...
- Android基础总结(十一)Fragment,动画
Fragment(重要) 用途:在一个Activity里切换界面,切换界面时只切换Fragment里面的内容 生命周期方法跟Activity一致,可以理解把其为就是一个Activity fragmen ...
- Windows 使用 Gitblit 搭建 Git 服务器
吐槽 Windows 安装 Git 那真是各种问题层出不穷,试过N种方法,反反复复不知道装了多少遍,然后又卸载.最后使用了Gitblit搭建Git服务器,虽然也遇到一些问题,但是都解决了.这个软件其实 ...
- Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法
Java,Mysql-根据一个给定经纬度的点,进行附近500米地点查询–合理利用算法 LBS 球面距离公式 http://wiki.myoa.info/zh-blog:20 Java,Mysql- ...
- C语言错误 指针的类型错误
//指针的类型错误 #include<stdio.h> #include<stdlib.h> #include<string.h> //用const来限制形参的指向 ...
- 如果输入参数采用“指针传递”,那么加 const 修饰可以防止意外地改动该指针,起 到保护作用
如果输入参数采用“指针传递”,那么加 const 修饰可以防止意外地改动该指针,起 到保护作用. #include <iostream> /* run this program using ...