concurrent.futures

concurrent.futures提供高层次的接口,用来实现异步调用。

这个异步执行可以使用threads(ThreadPoolExecutor)或者process(ProcessPoolExecutor)

这个feautre是Python3.2后的新功能,但是也支持Python2。

需要安装futures模块,https://pypi.python.org/pypi/futures/2.1.4

【例子1】非并发的例子

#!/usr/bin/env python2.6

from Queue import Queue
import random
import time q = Queue()
fred = [1,2,3,4,5,6,7,8,9,10] def f(x):
if random.randint(0,1):
time.sleep(0.1)
#
res = x * x
q.put(res) def main():
for num in fred:
f(num)
#
while not q.empty():
print q.get() if __name__ == "__main__":
main()

  

【例子2】使用ThreadPoolExecutor

#!/usr/bin/env python2.7                                                                                                     

from Queue import Queue
import concurrent.futures
import random
import time q = Queue()
fred = [1,2,3,4,5,6,7,8,9,10] def f(x):
if random.randint(0,1):
time.sleep(0.1)
#
res = x * x
q.put(res) def main():
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
for num in fred:
executor.submit(f, num)
#
while not q.empty():
print q.get() #################### if __name__ == "__main__":
main()

使用线程池中4个workers处理所有job。

with的语句保证所有线程都执行完成后,再进行下面的操作。

结果保持在一个队列中,队列是线程安全的。

. “The Queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implements all the required locking semantics.

队列模块实现多个生产者,多个消费者模式。特别在多线程之间进行信息交换的场景下最长使用。在这个模块下Queue类实现了所有需要的锁信息。

【例子3】使用ProcessPoolExecutor

The ProcessPoolExecutor class is an Executor subclass that uses a pool of processes to execute calls asynchronously. ProcessPoolExecutor uses the multiprocessing module, which allows it to side-step the Global Interpreter Lock but also means that only picklable objects can be executed and returned.

ProcessPoolExecute是Executor的子类,使用进程池实现异步调用。ProcessPoolExecute使用多进程模块,允许规避 Global Interpreter Lock,但是只有处理和返回picklable的对象。

#!/usr/bin/env python2.7

import sys
import redis
import concurrent.futures r = redis.Redis()
fred = [1,2,3,4,5,6,7,8,9,10] def check_server():
try:
r.info()
except redis.exceptions.ConnectionError:
print >>sys.stderr, "Error: cannot connect to redis server. Is the server running?"
sys.exit(1) def f(x):
res = x * x
r.rpush("test", res) def main():
with concurrent.futures.ProcessPoolExecutor(max_workers=4) as executor:
for num in fred:
executor.submit(f, num)
#
print r.lrange("test", 0, -1) #################### if __name__ == "__main__":
check_server()
###
r.delete("test")
main()

使用到redis链表的数据结构

Queue is not a good choice here because we are using processes here, and Queue is made for threads.

Queue不是一个好的选择,因为这里使用process。Queue是为线程准备的。

所以这里将结果存储在redis的list中,redis: getting started

在redis中所有的操作都是原子的,因此对于不同的进程可以安全写入相关的结果。

【测试】

1、把源数据设置为range(1,1000)之后,测试效果如下:

[root@typhoeus79 20140811]# time ./basic.py 

real    0m49.388s
user 0m0.024s
sys 0m0.013s
[root@typhoeus79 20140811]# time ./thread.py real 0m12.687s
user 0m0.103s
sys 0m0.061s
[root@typhoeus79 20140811]# time ./process.py real 0m0.507s
user 0m0.557s
sys 0m0.343s

  

【适应场景】

Threads are good for I/O tasks, while processes are good for CPU-bound tasks.

【Executor】

 class concurrent.futures.Executor

    An abstract class that provides methods to execute calls asynchronously. It should not be used directly, but through its concrete subclasses

Executor是一个抽象的类,提供执行异步调用的方法。不能直接调用,而是通过具体的子类来调用。

ThreadPoolExecutor和ProcessPoolExecutor都是其的子类。

 submit(fn, *args, **kwargs)

Schedules the callable, fn, to be executed as fn(*args **kwargs) and returns a Future object representing the execution of the callable.

执行函数fn(*args,**kwargs),返回一个Future对象,代表可调用的执行。

>>> with ThreadPoolExecutor(max_workers=1) as executor:
... future = executor.submit(pow, 323, 1235)
... print(future)
...
<Future at 0x7f1e7d053e10 state=finished returned long>

#打印结果
>>> with ThreadPoolExecutor(max_workers=1) as executor:
... future = executor.submit(pow, 323, 1235)
... print(future.result())
map(func, *iterables, timeout=None)

    Equivalent to map(func, *iterables) except func is executed asynchronously and several calls to func may be made concurrently. The returned iterator raises a TimeoutError if __next__() is called and the result isn’t available after timeout seconds from the original call to Executor.map(). timeout can be an int or a float. If timeout is not specified or None, there is no limit to the wait time. If a call raises an exception, then that exception will be raised when its value is retrieved from the iterator.

并发执行func,参数为iterables指定。timeout可以指定为int或者float类型,如果没有指定或者None,则无限等待。如果触发异常,当从iterator获取值的时候,这个异常将被捕获。

 shutdown(wait=True)

    Signal the executor that it should free any resources that it is using when the currently pending futures are done executing. Calls to Executor.submit() and Executor.map() made after shutdown will raise RuntimeError.

 释放资源使用。

使用with语句,避免该函数的调用,with语句会关闭所有的Executor。

>>> with ThreadPoolExecutor(max_workers=4) as e:
... e.submit(shutil.copy, 'src1.txt', 'dest1.txt')
... e.submit(shutil.copy, 'src2.txt', 'dest2.txt')
... e.submit(shutil.copy, 'src3.txt', 'dest3.txt')
... e.submit(shutil.copy, 'src3.txt', 'dest4.txt')
...
<Future at 0x7f1e79191250 state=running>
<Future at 0x7f1e79191450 state=finished raised IOError>
<Future at 0x7f1e79191250 state=running>
<Future at 0x7f1e79191450 state=finished raised IOError>

【参考文献】

1、https://pythonadventures.wordpress.com/tag/threadpoolexecutor/

2、https://docs.python.org/dev/library/concurrent.futures.html#module-concurrent.futures

concurrent.futures的更多相关文章

  1. Python标准模块--concurrent.futures

    1 模块简介 concurrent.futures模块是在Python3.2中添加的.根据Python的官方文档,concurrent.futures模块提供给开发者一个执行异步调用的高级接口.con ...

  2. 在python中使用concurrent.futures实现进程池和线程池

    #!/usr/bin/env python # -*- coding: utf-8 -*- import concurrent.futures import time number_list = [1 ...

  3. python简单粗暴多进程之concurrent.futures

    python在前面写过多线程的库threading: python3多线程趣味详解 但是今天发现一个封装得更加简单暴力的多进程库concurrent.futures: # !/usr/bin/pyth ...

  4. 45、concurrent.futures模块与协程

    concurrent.futures  —Launching parallel tasks    concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...

  5. python concurrent.futures

    python因为其全局解释器锁GIL而无法通过线程实现真正的平行计算.这个论断我们不展开,但是有个概念我们要说明,IO密集型 vs. 计算密集型. IO密集型:读取文件,读取网络套接字频繁. 计算密集 ...

  6. 进程池与线程池(concurrent.futures)

    from concurrent.futures import ProcessPoolExecutor import os,time,random def task(n): print('%s is r ...

  7. python异步并发模块concurrent.futures入门详解

    concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...

  8. Thread类的其他方法,同步锁,死锁与递归锁,信号量,事件,条件,定时器,队列,Python标准模块--concurrent.futures

    参考博客: https://www.cnblogs.com/xiao987334176/p/9046028.html 线程简述 什么是线程?线程是cpu调度的最小单位进程是资源分配的最小单位 进程和线 ...

  9. 线程池、进程池(concurrent.futures模块)和协程

    一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...

随机推荐

  1. jS判断浏览器终端

    在做移动端项目的时候,常常会遇到需要判断页面浏览终端的需求.要想判断是什么浏览器终端,先打印 navigator.userAgent 出来.所以收集了几种比较常用的方法: if(/(iPhone|iP ...

  2. Kettle文本文件输出和输入控件使用中,换行符导致的问题处理

    1.如下图通过输入控件从数据库读取数据然后生成TXT文本文件,TXT文件生成原则是每一条数据生成一行数据,第二条数据换行保存 2.如下图所示,使用文本文件输入控件读入上图生成的文件,文件读入原则是按行 ...

  3. Linux内存(手动释放cache)

    项目的扩容申请了一台机器,到手之后看一下机器的指标,看到内存使用情况是这样的. 1.查看内存 free $ free -h total used free shared buffers cached ...

  4. UWP 唤起应用商城,邮件

    UWP做到收尾工作的时候,一般需要在应用内做一个关于页面,用于放你的邮箱链接,商店评论链接等.. 一:打开链接 打开链接有两种做法 1.用 HyperlinkButton  (超链接按钮)这个控件,给 ...

  5. 【实验吧】Once More

    <?php if (isset ($_GET['password'])) { if (ereg ("^[a-zA-Z0-9]+$", $_GET['password']) = ...

  6. riot.js教程【一】简介

    Riotjs简介 Riotjs是一款简单的.优雅的.组件化UI前端开发框架: 他支持自定义标签(custom tags),拥有令人愉悦的语法,优雅的API和非常小的体积: 为什么需要一个新的界面库 前 ...

  7. Java命令模式以及来自lambda的优化

    前言    设计模式是软件工程中一些问题的统一解决方案的模型,它的出现是为了解决一些普遍存在的,却不能被语言特性直接解决的问题,随着软件工程的发展,设计模式也会不断的进行更新,本文介绍的是经典设计模式 ...

  8. win8.1 安装

    下载了Windows8.1企业版的iso文件,文件名称:cn_windows_8_1_enterprise_x86_dvd_2791409.iso 下载地址: http://msdn.itellyou ...

  9. Win10 UWP Intro to controls and events

    这篇翻译,如果有不对可以发邮箱 为创建页面,可以通过按钮,TextBox输入,组合框来显示数据,获得用户输入.添加一个控件可以使用三个关键步骤: 添加一个控件到界面 设置控件属性,高度,宽度,颜色 添 ...

  10. Kafka 学习笔记-基本概念

    一.基本概念 Kafka是一个分布式的,可分区的,可复制的消息系统 Kafka以由一个或多个服务以集群的方式运行,服务叫broker producer,consuer通过kafka topic发布,预 ...