concurrent.futures模块提供了高度封装的异步调用接口,它内部有关的两个池

ThreadPoolExecutor:线程池,提供异步调用,其基础就是老版的Pool

ProcessPoolExecutor: 进程池,提供异步调用

方法

ProcessPoolExecutor(n):n表示池里面存放多少个进程,之后的连接最大就是n的值

submit(fn,*args,**kwargs)  异步提交任务

map(func, *iterables, timeout=None, chunksize=1) 取代for循环submit的操作

shutdown(wait=True) 相当于进程池的pool.close()+pool.join()操作
wait=True,等待池内所有任务执行完毕回收完资源后才继续,--------》默认
wait=False,立即返回,并不会等待池内的任务执行完毕
但不管wait参数为何值,整个程序都会等到所有任务执行完毕
submit和map必须在shutdown之前

result(timeout=None) #取得结果 add_done_callback(fn) #回调函数
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
import time,random,os def task(n):
print('%s is running'% os.getpid())
time.sleep(random.randint(1,3))
return n**2
def handle(res):
res=res.result()
print("handle res %s"%res) if __name__ == '__main__':
# #同步调用
# pool=ProcessPoolExecutor(8)
#
# for i in range(13):
# pool.submit(task, i).result() #变成同步调用,串行了,等待结果
# # pool.shutdown(wait=True) #关门等待所有进程完成
# pool.shutdown(wait=False)#默认wait就等于True
# # pool.submit(task,3333) #shutdown后不能使用submit命令
#
# print('主') #异步调用
pool=ProcessPoolExecutor(8)
for i in range(13):
obj=pool.submit(task,i)
obj.add_done_callback(handle) #这里用到了回调函数
pool.shutdown(wait=True) #关门等待所有进程完成
print('主')

ProcessPoolExecutor

#提交任务的两种方式
#同步调用:提交完任务后,就在原地等待,等待任务结束,拿到任务的返回值,才能继续下一行代码,导致程序串行执行
#异步调用+回调机制:提交完任务后,不在原地等待,任务一旦执行完毕就会触发回调函数的执行,程序是并发执行 #同步有可能是计算任务而在等待
#ProcessPoolExcutor基于pool开发的 #进程的执行状态
#阻塞:遇到i/o进入的一种状态,等待
#非阻塞:

进程其他说明

from concurrent.futures import ThreadPoolExecutor
from urllib import request
from threading import current_thread
import time def get(url):
print('%s get %s'%(current_thread().getName(),url))
response=request.urlopen(url)
time.sleep(2)
# print(response.read().decode('utf-8'))
return{'url':url,'content':response.read().decode('utf-8')} def parse(res):
res=res.result()
print('parse:[%s] res:[%s]'%(res['url'],len(res['content']))) # get('http://www.baidu.com')
if __name__ == '__main__':
pool=ThreadPoolExecutor(2) urls=[
'https://www.baidu.com',
'https://www.python.org',
'https://www.openstack.org',
'https://www.openstack.org',
'https://www.openstack.org',
'https://www.openstack.org',
'https://www.openstack.org',
'https://www.openstack.org', ] for url in urls:
pool.submit(get,url).add_done_callback(parse)

ThreadPoolExecutor

from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor

import os,time,random
def task(n):
print('%s is runing' %os.getpid())
time.sleep(random.randint(1,3))
return n**2 if __name__ == '__main__': executor=ThreadPoolExecutor(max_workers=3) # for i in range(11):
# future=executor.submit(task,i) executor.map(task,range(1,12)) #map取代了for+submit

map用法

回调 略

concurrent.futures模块 -----进程池 ---线程池 ---回调的更多相关文章

  1. 使用concurrent.futures模块中的线程池与进程池

    使用concurrent.futures模块中的线程池与进程池 线程池与进程池 以线程池举例,系统使用多线程方式运行时,会产生大量的线程创建与销毁,创建与销毁必定会带来一定的消耗,甚至导致系统资源的崩 ...

  2. concurrent.futures模块(进程池&线程池)

    1.线程池的概念 由于python中的GIL导致每个进程一次只能运行一个线程,在I/O密集型的操作中可以开启多线程,但是在使用多线程处理任务时候,不是线程越多越好,因为在线程切换的时候,需要切换上下文 ...

  3. concurrent.futures模块(进程池/线程池)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

  4. Python标准模块--concurrent.futures(进程池,线程池)

    python为我们提供的标准模块concurrent.futures里面有ThreadPoolExecutor(线程池)和ProcessPoolExecutor(进程池)两个模块. 在这个模块里他们俩 ...

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

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

  6. Python并发编程之线程池/进程池--concurrent.futures模块

    一.关于concurrent.futures模块 Python标准库为我们提供了threading和multiprocessing模块编写相应的多线程/多进程代码,但是当项目达到一定的规模,频繁创建/ ...

  7. Python标准模块--concurrent.futures 进程池线程池终极用法

    concurrent.futures 这个模块是异步调用的机制concurrent.futures 提交任务都是用submitfor + submit 多个任务的提交shutdown 是等效于Pool ...

  8. 使用concurrent.futures模块并发,实现进程池、线程池

    Python标准库为我们提供了threading和multiprocessing模块编写相应的异步多线程/多进程代码 从Python3.2开始,标准库为我们提供了concurrent.futures模 ...

  9. 创建进程池与线程池concurrent.futures模块的使用

    一.进程池. 当并发的任务数量远远大于计算机所能承受的范围,即无法一次性开启过多的任务数量就应该考虑去 限制进程数或线程数,从而保证服务器不会因超载而瘫痪.这时候就出现了进程池和线程池. 二.conc ...

随机推荐

  1. 学习大数据基础框架hadoop需要什么基础

    什么是大数据?进入本世纪以来,尤其是2010年之后,随着互联网特别是移动互联网的发展,数据的增长呈爆炸趋势,已经很难估计全世界的电子设备中存储的数据到底有多少,描述数据系统的数据量的计量单位从MB(1 ...

  2. PageBaseType属性的功用

    在web.config中经常能看到如下类似语句:<pages theme="Default"   pageBaseType="VS.Facade.PageBase, ...

  3. Spark版本说明

    Source code: Spark 源码,需要编译才能使用,另外 Scala 2.11 需要使用源码编译才可使用   Pre-build with user-provided Hadoop: &qu ...

  4. sbt第一次运行下载jar包很慢解决办法

    一.补充sbt配置文件,添加下载路径 文件结构如下:修改了sbtconfig.txt,repo.properties. sbtconfig.txt配置内容为: # Set the java args  ...

  5. Avalon总线学习 ---Avalon Interface Specifications

    Avalon总线学习 ---Avalon Interface Specifications 1.Avalon Interfaces in a System and Nios II Processor ...

  6. 【转】前端Web开发MVC模式-入门示例

    前端Web开发MVC模式-入门示例 MVC概论起初来之桌面应用开发.其实java的structs框架最能体现MVC框架:model模型是理解成服务器端的模块程序:view为发送给客服端的内容:cont ...

  7. gcc与g++的一些关系

    Gcc 简介Linux系统下的gcc(GNU C Compiler)是GNU推出的功能强大.性能优越的多平台编译器,是GNU的代表作品之一.Gcc是可以在多种硬体平台上编译出可执行程序的超级编译器,其 ...

  8. 黑域,黑阈 Permission denied

    在执行: adb -d shell sh /data/data/me.piebridge.brevent/brevent.sh 时遇到Permission denied,多运行一次就好了. 完整的有两 ...

  9. dom响应事件

    DOMsubtreeModified.DOMNodeInserted.DOMNodeRemoved.DOMAttrModified.DOMCharacterDataModified 当底层DOM结构发 ...

  10. jquery zTree异步搜索的例子--搜叶子节点

    参考博客:https://www.cnblogs.com/henuyuxiang/p/6677397.html 前台代码 <%@ page language="java" c ...