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 ...
随机推荐
- Ionic JPush极光推送二
1.看图解决问题 2.解决出现统计代码提示问题 修改这个java 文件 导入命名空间 import cn.jpush.android.api.JPushInterface; 添加方法 @Overr ...
- ES6 class继承
ES6 class继承 class类的继承 class可以通过extends关键字实现继承,这笔ES5的通过修改原型连实现继承要清晰和方便很多. class Point{ } class ColorP ...
- IIS发布web应用程序之再折腾
最近几个月发布程序比较多,遇到了各种IIS发布web程序后无法访问的问题.原以为对各种问题都已经摸的差不多了,但今天又为一问题折腾了大半天.具体过程祥记如下: 在server2008 R2 64位系统 ...
- JEECMS自定义标签开发步骤2
JEECMS自带的只有[@cms_advertising]标签,并且官方没有给文档,用法: [@cms_advertising id='3'] <img src=&quo ...
- 取消 ios 上下滑动
- c++ string 字符串
转载 http://www.renfei.org/blog/introduction-to-cpp-string.html 运算符重载 + 和 +=:连接字符串 =:字符串赋值 >.> ...
- PAT甲级——A1013 Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- hbase表内存的分布
- 对于ssm过程中的乱码问题的处理
首先是数据库乱码问题: 1.可以先检测一下是否是数据库的问题: 可以先输入查询语句SHOW VARIABLES LIKE 'character_set_%';,查看所有的编码是否是UTF-8. (一般 ...
- Vue开发警告[Vue warn]: Avoid replacing instance root $data. Use nested data properties instead.
Avoid replacing instance root $data. Use nested data properties instead. 翻译 避免替换实例根$data.请改用嵌套数据属性 错 ...