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模块的更多相关文章

  1. Python程序中的线程操作(线程池)-concurrent模块

    目录 Python程序中的线程操作(线程池)-concurrent模块 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecut ...

  2. Python程序中的线程操作-concurrent模块

    目录 一.Python标准模块--concurrent.futures 二.介绍 三.基本方法 四.ProcessPoolExecutor 五.ThreadPoolExecutor 六.map的用法 ...

  3. Python开发【模块】:Concurrent

    concurrent 模块 回顾: 对于python来说,作为解释型语言,Python的解释器必须做到既安全又高效.我们都知道多线程编程会遇到的问题,解释器要留意的是避免在不同的线程操作内部共享的数据 ...

  4. python并发模块之concurrent.futures(一)

    Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threadin ...

  5. python的并发模块concurrent

    Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threadin ...

  6. Haskell语言学习笔记(84)Concurrent

    Control.Concurrent Prelude> import Control.Concurrent Prelude Control.Concurrent> Control.Conc ...

  7. python:常用模块 知识整理

    time模块 time.time() # 时间戳:1487130156.419527 time.strftime("%Y-%m-%d %X") #格式化的时间字符串:'2017-0 ...

  8. Concurrent.Thread.js

    (function(){ if ( !this.Data || (typeof this.Data != 'object' && typeof this.Data != 'functi ...

  9. 使用express4.X + jade + mongoose + underscore搭建个人电影网站

    (-。-;), 周末过得真是快啊,  很久以前就看到imooc上有个搭建个人电影网站一期 ,二期的视频, 这两周宅家里撸玩没事干, 我也学着搭了一个, 这些东西都是基础, 只要花点时间很好学的, no ...

随机推荐

  1. nextjs服务端渲染原理

    1. 简单的介绍一下 nextjs是react进行服务端渲染的一个工具,默认以根目录下的pages为渲染路由 比如我在pages目录下创建一个index.js文件,然后export default一个 ...

  2. Tool-MySQL-SQLyog:SQLyog

    ylbtech-Tool-MySQL-SQLyog:SQLyog SQLyog 是一个快速而简洁的图形化管理MYSQL数据库的工具,它能够在任何地点有效地管理你的数据库,由业界著名的Webyog公司出 ...

  3. 为什么@RestController返回的Date类型是Long型

    最近项目中发现一个好玩的事情:本地调试时,返回的date是日期格式yyyy-MM-dd HH:mm:ss,但发布到服务器后就变为Long型的时间戳了 最后查到的原因很简单,因为发布服务器启动时的脚本加 ...

  4. jeecms v9导入myeclipse 2015 ehcache.xml报错问题

    1.找不到ehcache.xml文件问题 cache-context.xml <property name="configLocation"> <value> ...

  5. 逻辑备份(mysqldump/select into outfile)

    #mysqldump备份 shell> mysqldump -uroot -p -P4306 sakila actor>E:\sakila-actor.sql shell> mysq ...

  6. PHP实现定时任务(非linux-shell方式,与操作系统无关)

    页面交互效果 下面是写好的示例前端交互页面,主要是列表页面,编辑页面. 主要交互有: 1 开启定时任务进程 2 关闭定时任务进程 3 新增一项定时任务 4 编辑已有的定时任务 5 删除定时任务 6 开 ...

  7. bzoj 2935 [Poi1999]原始生物——欧拉回路思路!

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2935 有向图用最小的路径(==总点数最少)覆盖所有边. 完了完了我居然连1999年的题都做不 ...

  8. 傻瓜式教程--实现登录页面的验证码以及验证(VUE)

    做成之后就 是这个样子 接下来上代码创建一个组件.显示验证码图片 <template> <div class="s-canvas"> <canvas ...

  9. java开发岗位面试整理

    一.Java基础 1. String类为什么是final的 2. HashMap的源码,实现原理,底层结构. 3. 说说你知道的几个Java集合类:list.set.queue.map实现类. 4. ...

  10. GIT生成公钥和私钥

    转载至:https://blog.csdn.net/gwz1196281550/article/details/80268200 打开 git bash! git config --global us ...