使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类
类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类。
使并发核心池能够在 threadpoolexetor和geventpoolexecutor自由选一种切换。
实现方式。
- # -*- coding: utf-8 -*-
- # @Author : ydf
- # @Time : 2019/7/2 14:11
- import atexit
- import time
- import warnings
- from collections import Callable
- import gevent
- from gevent import pool as gevent_pool
- from gevent import monkey
- from gevent.queue import JoinableQueue
- from app.utils_ydf import LoggerMixin, nb_print, LogManager
- def check_gevent_monkey_patch(raise_exc=True):
- if not monkey.is_module_patched('socket'):
- if raise_exc:
- warnings.warn(f'检测到 你还没有打gevent包的猴子补丁,请在所运行的脚本第一行写上 【import gevent.monkey;gevent.monkey.patch_all()】 这句话。')
- raise Exception(f'检测到 你还没有打gevent包的猴子补丁,请在所运行的脚本第一行写上 【import gevent.monkey;gevent.monkey.patch_all()】 这句话。')
- else:
- return 1
- logger_gevent_timeout_deco = LogManager('logger_gevent_timeout_deco').get_logger_and_add_handlers()
- def gevent_timeout_deco(timeout_t):
- def _gevent_timeout_deco(f):
- def __gevent_timeout_deceo(*args, **kwargs):
- timeout = gevent.Timeout(timeout_t, )
- timeout.start()
- try:
- f(*args, **kwargs)
- except gevent.Timeout as t:
- logger_gevent_timeout_deco.error(f'函数 {f} 运行超过了 {timeout_t} 秒')
- if t is not timeout:
- nb_print(t)
- # raise # not my timeout
- finally:
- timeout.close()
- return __gevent_timeout_deceo
- return _gevent_timeout_deco
- class GeventPoolExecutor(gevent_pool.Pool):
- def __init__(self, size=None, ):
- check_gevent_monkey_patch()
- super().__init__(size, )
- def submit(self, *args, **kwargs):
- self.spawn(*args, **kwargs)
- def shutdown(self):
- self.join()
- if __name__ == '__main__':
- monkey.patch_all()
- def f2(x):
- time.sleep(1)
- nb_print(x)
- pool = GeventPoolExecutor(4)
- for i in range(15):
- nb_print(f'放入{i}')
- pool.submit(gevent_timeout_deco(8)(f2), i)
- nb_print(66666666)
对于收尾任务,threadpoolexecutor和这个还有少量不同,这个geventpool在脚本退出前不去主动join(shutdown)他,最后四个任务就会丢失 。
threadpoolexecutor起的是守护线程,按道理也会出现这样的结果,但是concurrent包里面做了atexit处理。这里也可以使用atexit.register注册shutdown达到同样的目的,不需要手动调用join防止脚本提前退出。
实现eventlet的核心池,同理。
使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类的更多相关文章
- 使用evenlet包实现 concurrent.futures.executor包的鸭子类
适配成同一个同样的公有方法. # -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/7/3 10:35 import time import w ...
- python异步并发模块concurrent.futures入门详解
concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- concurrent.futures模块简单介绍(线程池,进程池)
一.基类Executor Executor类是ThreadPoolExecutor 和ProcessPoolExecutor 的基类.它为我们提供了如下方法: submit(fn, *args, ** ...
- 线程与进程 concurrent.futures模块
https://docs.python.org/3/library/concurrent.futures.html 17.4.1 Executor Objects class concurrent.f ...
- python concurrent.futures包使用,捕获异常
concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池.和另外一个第三方threadpool包相比,这个可以非阻塞的 ...
- (11)线程池(最新的concurrent.futures包去开启)
'''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- concurrent.futures模块(进程池/线程池)
需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...
随机推荐
- 记录一次win2003服务器的IIS服务加载.flv后缀的资源报错404的处理方法
问题:访问某个域名下的xxxx.flv资源,页面报错404. 解决思路: 1.权限是否给足 user权限给完全控制咯 如果你访问该域名下的其他资源无问题的话就不是介个原因了 2.MIME类型是否少了 ...
- C#控制多线程最大并行数量
- BitMap 图片格式与Base64Image格式互转方法
BitMap 图片格式与Base64Image格式互转方法 /// <summary> /// 图片转为base64编码的字符串 /// </summary> /// < ...
- destoon6.0 手机版添加下载模块
common.inc.php 里面加入 下载板块的down 名称 在include 文件里 加入 down.inc.php 文件 在 mobile模版里 加入 down.htm 模版文件 在 电脑版 ...
- SpringBoot——SpringBoot学习记录【一】
前言 公司目前主要的业务,用的语言是java,所以学习下相关的技术呀,还好大学基础语言学的JAVA SpringBoot简介 官网 SpringBoot 简介 SpringBoot是用来简化Sprin ...
- Ofbiz项目学习——阶段性小结——查询
一.组装参数的学习 首先是查询条件,对于查询条件,需要判断是否从前端传递空值?——怎么处理查询空值? 当然可以一个一个进行判断,但是这样代码会导致很多,可以统一处理,形成一个公共方法. 1. 单个处理 ...
- redux沉思录
要素:store.reducer.dispatch/subscribe connect:将业务逻辑剥离到容器类,数据的双向绑定: 数据.操作.UI分离.命令封装 核心思想:对共享状态的维护: 核心代码 ...
- RxSwift 在本质上简化了开发异步程序
RxSwift 是一个组合异步和事件驱动编程的库,通过使用可观察序列和功能样式运算符来,从而允许通过调度程序进行参数化执行. RxSwift 在本质上简化了开发异步程序,允许代码对新数据作出反应,并以 ...
- linux学习13 Linux运维常用文件管理命令及系统变量基础
一.文件管理命令 1.cp命令,copy a.单源复制,cp [OPTION]... [-T] SOURCE DEST 如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中. 如果D ...
- 使用google autoservice 自动生成java spi 描述文件
spi 是一种服务发现的标准,对于开发中我们通常需要编写 META-INF/services 文件夹中定义的类. google auto 中的autoservice 可以帮助我们生成对应的配置,很方便 ...