concurrent.futures

先看下官方介绍

The asynchronous execution can be performed with threads, using ThreadPoolExecutor, or separate processes, using ProcessPoolExecutor. Both implement the same interface, which is defined by the abstract Executor class.

重点是 asynchronous

concurrent.futures._base.Executor

submit(fn, *args, **kwargs) -> Future

with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(pow, 323, 1235)
print(future.result())

map(func, *iterables, timeout=None, chunksize=1) -> typing.Generator

类似于 map(内置函数)

In [26]: def test(item):
...: print("echo", item)
...: return item
...:
In [27]: list(map(test, range(10)))
echo 0
echo 1
echo 2
echo 3
echo 4
echo 5
echo 6
echo 7
echo 8
echo 9
Out[27]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  • demo
def test(item):
print(f"echo {item}")
return item
with ThreadPoolExecutor(max_workers=1) as executor:
future = executor.map(test, range(3))

shutdown

# todo

ThreadPoolExecutor

  • 注意避免在一个 Future 里面获取另外一个 Future 的 result

ProcessPoolExecutor

在 IO 密集型下使用 ThreadPoolExecutor

Future

  • cancel()

    • 无法取消当前正在执行的
  • result() -> bool
  • running() -> bool
  • done() -> bool
  • result(timeout=None) -> Any
  • exception(timeout=None) -> Exception
exector = ThreadPoolExecutor(max_workers=10)
f: Future = exector.submit(test, 1)
exception: Exception = f.exception(timeout=None)
  • add_done_callback(fn) -> None
def callback(*args):
print(args) # (<Future at 0x102bcb898 state=finished raised KeyError>,) 即 f exector = ThreadPoolExecutor(max_workers=10)
f: Future = exector.submit(test, 1)
f.add_done_callback(callback)

concurrent.futures 学习笔记的更多相关文章

  1. java.util.concurrent包学习笔记(一)Executor框架

    类图: 其实从类图我们能发现concurrent包(除去java.util.concurrent.atomic 和 java.util.concurrent.locks)中的内容并没有特别多,大概分为 ...

  2. java Concurrent包学习笔记(一):ExecutorService

    一.介绍 ExecutorService是java.util.concurrent包中的一个线程池实现接口.其有两个实现类: 1)ThreadPoolExecutor:普通线程池通过配置线程池大小,能 ...

  3. java Concurrent包学习笔记(六):Exchanger

    一.概述 Exchanger 是一个用于线程间协作的工具类,Exchanger用于进行线程间的数据交换,它提供一个同步点,在这个同步点,两个线程可以交换彼此的数据.这两个线程通过exchange 方法 ...

  4. java Concurrent包学习笔记(五):Semaphore

    一.Semaphore 是什么  信号量Semaphore是一个并发工具类,用来控制可同时并发的线程数,其内部维护了一组虚拟许可,构造函数初始化的时候可以指定许可的总数量 每次线程执行操作时先通过ac ...

  5. java Concurrent包学习笔记(三):ReentrantLock

    一.可重入性的理解 从名字上理解,ReenTrantLock的字面意思就是再进入的锁,其实synchronized关键字所使用的锁也是可重入的,两者关于这个的区别不大.两者都是同一个线程每进入一次,锁 ...

  6. java Concurrent包学习笔记(二):CountDownLatch和CyclicBarrier

    一.CountDownLatch CountDownLatch一个线程同步的工具,是的一个或者多个线程等待其他线程操作完成之后再执行. CountDownLatch通过一个给定的数值count来进行初 ...

  7. java Concurrent包学习笔记(四):BlockingQueue

    一.BlockingQueue概述 1.阻塞的含义 BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞.被阻塞的情况主要有如下两种: ,当一个线程对 ...

  8. java Concurrent包学习笔记(七):ConcurrentHashMap

    (注意:以下讲解的ConcurrentHashMap是jdk 1.8的) 一.ConcurrentHashMap的数据结构 ConcurrentHashMap在1.8中的实现,相比于1.7的版本基本上 ...

  9. Python开发【笔记】:concurrent.futures 平行运算

    平行运算 前言: 编写Python程序时,我们可能会遭遇性能问题,即使优化了代码,程序也依然有可能运行的很慢,从而无法满足我们对执行速度的要求,目前的计算机,其cpu核心数越来越多,于是,我们可以考虑 ...

随机推荐

  1. UEditor使用总结(与SpringMVC整合)

    最近再弄富文本框,选择了UEditor,原因是:界面漂亮,百度开源的然而, 开启整合之路(怎么做) 1.下载插件 下载只有将插件放在Webapp下,如图 2.修改 导入之后我们就需要修改一些参数已满足 ...

  2. 念念不忘SERVLET

    这个弄弄也有意思,以前无法入门,没有系统性概念,现在慢慢开始懂了.. 这个SERVLET/JSP学习笔记也易懂.. 那个JAVA7程序设计也可以慢慢看来,, 再加上SPRING,我黑心了??:) pa ...

  3. SQL优化(SQL TUNING)之10分钟完毕亿级数据量性能优化(SQL调优)

    前几天.一个用户研发QQ找我,例如以下: 自由的海豚. 16:12:01 岛主,我的一条SQL查不出来结果,能帮我看看不? 兰花岛主 16:12:10 多久不出结果? 自由的海豚 16:12:17 多 ...

  4. MySQL改动rootpassword的多种方法

     方法1: 用SET PASSWORD命令 mysql -u root mysql> SET PASSWORD FOR 'root'@'localhost' = PASSWORD('newp ...

  5. [Javascript Crocks] Recover from a Nothing with the `alt` method

    Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...

  6. [Angular] Performance Caching Policy - Cache First, Network Last

    If you want to cache API response by using angular service-worker, you can do it in: src/ngsw-config ...

  7. 301 和 302 对 SEO 的影响

    网站优化中,经常会面临网站链接修改或改变的事情,其中一个解决办法就是使用网站跳转的方式,处理变化的链接,下面讲述301和302跳转对SEO的影响. 301(永久移动) 请求的网页已被永久移动到新位置. ...

  8. Working with SQL Server LocalDB

    https://docs.asp.net/en/latest/tutorials/first-mvc-app/working-with-sql.html The ApplicationDbContex ...

  9. php创建简单的列表页

    php创建简单的列表页 样例 代码 <?php $userInfo[] = array( 'id'=>'1', 'username'=>'fry', 'sex'=>'nan', ...

  10. js例子

    1.子菜单下拉 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www ...