使用evenlet包实现 concurrent.futures.executor包的鸭子类
适配成同一个同样的公有方法。
# -*- coding: utf-8 -*-
# @Author : ydf
# @Time : 2019/7/3 10:35
import time
import warnings from eventlet import greenpool, monkey_patch, patcher, Timeout from app.utils_ydf import LogManager, nb_print def check_evenlet_monkey_patch(raise_exc=True):
if not patcher.is_monkey_patched('socket'):
if raise_exc:
warnings.warn(f'检测到没有打 evenlet 包的猴子补丁 ,请在文件首行加上 import eventlet;eventlet.monkey_patch(all=True) ')
raise Exception('检测到没有打 evenlet 包的猴子补丁 ,请在文件首行加上 import eventlet;eventlet.monkey_patch(all=True)')
else:
return 1 logger_evenlet_timeout_deco = LogManager('logger_evenlet_timeout_deco').get_logger_and_add_handlers() def evenlet_timeout_deco(timeout_t):
def _evenlet_timeout_deco(f):
def __evenlet_timeout_deco(*args, **kwargs):
timeout = Timeout(timeout_t, )
# timeout.start() # 与gevent不一样
try:
f(*args, **kwargs)
except Timeout as t:
logger_evenlet_timeout_deco.error(f'函数 {f} 运行超过了 {timeout_t} 秒')
if t is not timeout:
nb_print(t)
# raise # not my timeout
finally:
timeout.cancel() return __evenlet_timeout_deco return _evenlet_timeout_deco class CustomEventletPoolExecutor(greenpool.GreenPool):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
check_evenlet_monkey_patch() def submit(self, *args, **kwargs): # 保持为一直的公有用法。
# nb_print(args)
self.spawn_n(*args, **kwargs)
# self.spawn_n(*args, **kwargs) def shutdown(self):
self.waitall() if __name__ == '__main__':
# greenpool.GreenPool.waitall()
monkey_patch(all=True) def f2(x): time.sleep(1)
nb_print(x) pool = CustomEventletPoolExecutor(4) for i in range(15):
nb_print(f'放入{i}')
pool.submit(evenlet_timeout_deco(0.8)(f2), i)
使用evenlet包实现 concurrent.futures.executor包的鸭子类的更多相关文章
- 使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类
类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类. 使并发核心池能够在 threadpoolexetor和geventpoolexecu ...
- python异步并发模块concurrent.futures入门详解
concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- 线程与进程 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 #开启线 ...
- Storm/Cassandra集成错误:NoSuchMethodError: concurrent.Futures.withFallback
本文原文出处: http://blog.csdn.net/bluishglc/article/details/50443205 严禁不论什么形式的转载.否则将托付CSDN官方维护权益. 2015年的最 ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- Python标准模块--concurrent.futures
1 模块简介 concurrent.futures模块是在Python3.2中添加的.根据Python的官方文档,concurrent.futures模块提供给开发者一个执行异步调用的高级接口.con ...
随机推荐
- zabbix--显示插件Graphtree
zabbix配置显示插件Graphtree 网盘下载地址:https://pan.baidu.com/s/1HGALOq2z7XiVRxyb6ASAeQ 提取码:13vn Graphtree 安装 ...
- 小型SSM项目出现Failed to load ApplicationContext错误的解决方法(个人向)
使用单元测试的时候,出现了Failed to load ApplicationContext错误,在添加了一个新的Mapper.xml文件才出现的,在保证其他配置文件没有出错的情况下,检查mapper ...
- Win10 家庭版 VMware 无法启动 解决办法
引发原因 最近更新了一个补丁 KB4524147 安装后会导致 VM 无法打开(如果你没有安装hyper-v的话) 解决方案 控制面板 -> 程序 -> 查看已安装的更新 -> 找到 ...
- XXX接口自动化测试方案
XXX接口自动化测试方案 1.引言 1.1 文档版本 版本 作者 审批 备注 V1.0 XXXX 创建测试方案文档 1.2 项目情况 项目名称 XXX 项目版本 V1.0 项目经理 XX 测试人员 X ...
- Python决策树可视化:GraphViz's executables not found的解决方法
参考文献: [1]Python决策树可视化:GraphViz's executables not found的解决方法
- django常用命令行和一些笔记
命令行 新建项目:django-admin startproject projectname 新建应用:python manage.py startapp appname(每次创建了新的app后,都需 ...
- 【Spark】ScalaIDE运行spark,A master URL must be set in your configuration
or SparkSession.master("local")
- Python中*args和**kargs如何使用
对于初学者来说,看到*args和**kargs就头大,到底它们有何用处,怎么使用?本文将和大家一起来扒一扒,希望对大家学习python有所帮助. 1.*args 实质就是将函数传入的参数,存储在元组类 ...
- int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restric mutex)
mutex:为了保护条件变量而存在的: cond:为了线程通信而存在的. 整个机制都是为了保护条件变量和线程间通信而存在的. pthread_cond_wait()函数一进入wait状态就会自动rel ...
- EJS的个人总结
什么是模板引擎? 用于Web开发的模板引擎是为了使用用户界面与业务数据(内容)分离而产生的,使用模板语法编写的模板代码通常放在具有特的格式的文档中,经过模板引擎编译之后就会生成一个标准的HTML文档. ...