使用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 ...
随机推荐
- kuangbin专题专题四 MPI Maelstrom POJ - 1502
题目链接:https://vjudge.net/problem/POJ-1502 dijkstra板子题,题目提供下三角情况,不包含正对角线,因为有题意都为0,处理好输入,就是一个很水的题. #inc ...
- ansible(二)
软件相关模块 yum rpm和yum的区别 rpm:redhat package manager yum可以解决依赖关系 yum源配置 [epel] name=Extra Packages - $ba ...
- ImportError: No module named wx
先检查下Python中是否有此模块 $ python >>> import wx ImportError: No module named wx wx模块的确没有安装. 要安装wx ...
- GC线程是不是守护线程
是 线程的话分为守护线程和非守护线程(即用户线程) 只要当前JVM实例中尚存在任何一个非守护线程没有结束,守护线程就全部工作; 只有当最后一个非守护线程结束时,守护线程随着JVM一同结束工作,守护线程 ...
- Win10-无法启动虚拟机
环境:Win10 - 1903 Q1:安装使用Hyper-V后,安装Virtual box新建VM,无法启动 A1:卸载Hyper-V(非必须) 管理员运行ps/cmd bcdedit /set ...
- 使用 application.properties 中配置的属性,举例:@Value("${server.port}")
使用 application.properties 中配置的属性:@Value 注解. @RestController public class HelloWorldController { @Val ...
- 【Android Studio】 资源下载
Gradle各版本集合 http://services.gradle.org/distributions/ Android Studio各版本集合 http://www.android-studio. ...
- 关于Java锁(学习笔记)
个人学习笔记! 1)分布式锁的实现?①数据库实现单点.非重入.非阻塞.无失效时间.依赖数据库(要自己设置,可结合排它锁.乐观锁.悲观锁等混合使用)②缓存(Redis等)集群部署解决单点问题.分布式锁方 ...
- BlocksKit block从配角到主角—oc通往函数式编程之路--oc rx化?
BlocksKit 对 oc语言的功能层.UI层进行了大量的适配,使得oc能够复合函数式编程的风格: 是oc语言的函数式编程风格改造: 将函数式风格的BlocksKit API解释为原生的功能调用: ...
- React.js 小书
http://huziketang.mangojuice.top/books/react/