concurrent模块
concurrent包
concurrent.futrues模块
3.2版本引入
异步并行任务模块,提供一个高级的异步可执行的便利接口.
提供了两个池执行器
ThreadPoolExecutor异步调用的线程池的Executor
ProcessPoolExecutor异步调用的进程池的Executor
ThreadPoolExecutor对象
首先需要定义一个池的执行器对象,Executor类子类对象
| 方法 | 含义 |
|---|---|
| ThreadPoolExecutor(max_workers= 1) | 池中最多创建max_workers个线程的池类同时异步执行,返回Executor实例 |
| submit(fn,*args,**kwargs) | 异步提交执行的函数及其参数,返回Future实例 |
| shutdown(wait = True) | 清理池 |
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import time
# ProcessPoolExecutor(5) # 5代表只能开启5个进程
# ProcessPoolExecutor() # 默认以CPU的个数限制进程数
pool = ThreadPoolExecutor(5) # 5代表只能开启5个线程
# ThreadPoolExecutor() # 默认以CPU个数 * 5 限制线程数
# t = Tread() # 异步提交
# t.start(0)
Future类
| 方法 | 含义 |
|---|---|
| result(timeout=None) | 可以查看调用函数的返回值结果,tiime未none,一直等待返回;timeout设置到期,抛出concurrent.futures.TimeError异常 |
| done() | 如果调用被成功的取消或者执行完成,返回True |
| cancelled() | 如果调用被成功的取消,返回True |
| result() | 如果正在运行且不能被取消,返回True |
| cancel() | 尝试取消调用,如果已经执行且不能取消放回false;否则返回True |
| exception(timeout= None) | 取返回的异常,timeout为None,一直等待返回;timeout设置到期,抛出concurrent.futures.TimeoutError异常 |
#pip3 install gevent
from gevent import monkey
monkey.patch_all() # 可以监听该程序下所有的IO操作
import time
from gevent import spawn, joinall # 用于做切换 + 保存状态
def func1():
print('1')
# IO操作
time.sleep(1)
def func2():
print('2')
time.sleep(3)
def func3():
print('3')
time.sleep(5)
start_time = time.time()
s1 = spawn(func1)
s2 = spawn(func2)
s3 = spawn(func3)
# s2.join() # 发送信号,相当于等待自己 (在单线程的情况下)
# s1.join()
# s3.join()
# 必须传序列类型
joinall([s1, s2, s3])
end_time = time.time()
print(end_time - start_time)
ProcessPoolExecutor对象
跟线程方法一样,就是使用多进程完成.
支持上下文管理
concurrent.futrues.ProcessPoolExecutor继承自concurrent.futrues.base.Executor,
支持上下文管理,可以使用with语句
回调函数
将一个函数的返回值直接传递给另一个函数
# 回调函数
def call_back(res):
print(type(res))
# 注意: 赋值操作不要与接收的res同名
res2 = res.result()
print(res2)
for line in range(5):
pool.submit(task, 1).add_done_callback(call_back)
# 会让所有线程池的任务结束后,才往下执行代码
# pool.shutdown()
print('hello')
总结
这个库统一了线程池,进程池的调用,简化了编程
唯一缺点:无法设置线程名称,无所吊谓
concurrent模块的更多相关文章
- Python程序中的线程操作(线程池)-concurrent模块
目录 Python程序中的线程操作(线程池)-concurrent模块 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecut ...
- Python程序中的线程操作-concurrent模块
目录 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecutor 五.ThreadPoolExecutor 六.map的用法 ...
- Python开发【模块】:Concurrent
concurrent 模块 回顾: 对于python来说,作为解释型语言,Python的解释器必须做到既安全又高效.我们都知道多线程编程会遇到的问题,解释器要留意的是避免在不同的线程操作内部共享的数据 ...
- python并发模块之concurrent.futures(一)
Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threadin ...
- python的并发模块concurrent
Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threadin ...
- Haskell语言学习笔记(84)Concurrent
Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...
- python:常用模块 知识整理
time模块 time.time() # 时间戳:1487130156.419527 time.strftime("%Y-%m-%d %X") #格式化的时间字符串:'2017-0 ...
- Concurrent.Thread.js
(function(){ if ( !this.Data || (typeof this.Data != 'object' && typeof this.Data != 'functi ...
- 使用express4.X + jade + mongoose + underscore搭建个人电影网站
(-。-;), 周末过得真是快啊, 很久以前就看到imooc上有个搭建个人电影网站一期 ,二期的视频, 这两周宅家里撸玩没事干, 我也学着搭了一个, 这些东西都是基础, 只要花点时间很好学的, no ...
随机推荐
- charles for https
To remotely capture http or https traffic with charles you will need to do the following: HOST - Mac ...
- SpringMVC配置顺序的问题
1:web.xml:web应用一经加载,先来找他 1):指明applicationContext的位置 2):引入spring监听,ContextLoaderListe ...
- 从登录接口的响应结果里提取token
token一般存在于2个地方:1. cookie, 2 ,某个接口的响应结果中 1. 我们接口的token存在于登录接口的响应结果中,如下图: token值 为红色标记的值,在登录接口里加以下2行代码 ...
- leetcode 843. Guess the Word
我做过的第一个 interactive problem 给一个候选词列表,每次猜测可以猜里面的词,会返回猜中匹配的个数, 可以猜10次, 加上随机化策略之后几乎可以一定通过测试(尽管不是100%) c ...
- 技术人自己的KPI
为什么需要技术KPI 在业务技术团队,有一个不好的趋势,就是团队越来越业务,越来越没有技术味道.每个人都在谈业务,技术大会上在谈业务,周会上在聊业务,周报里写的是业务项目......唯独少被谈及的是技 ...
- SSM2-搭建maven常见的错误以及解决方法
1.项目中的jdk版本电脑自带的jdk版本不一致 mavne-parent 的pom.xml文件 <!-- java编译插件 --> <plugin> <groupId& ...
- vue 安装 fontawesome
查看最新版的fontawesome 信息: https://github.com/FortAwesome/vue-fontawesome 搜索图标 :https://fontawesome.com/i ...
- H5C3--过渡transition
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU1709
/* * 好奇怪的母函数 */ #include<cstdio> #include<cstring> #include<cmath> #include<a ...
- tesseract ocr训练 pt验证码
识别率有问题A大概率识别为n,因此需要训练,这里讲一下 如何训练 参考 java代码里边直接使用tess4j,是对tesseract的封装,但是如果要训练,还是需要在进行安装tesseract-ocr ...