适配成同一个同样的公有方法。

# -*- 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包的鸭子类的更多相关文章

  1. 使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类

    类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类. 使并发核心池能够在 threadpoolexetor和geventpoolexecu ...

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

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

  3. concurrent.futures

    concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...

  4. 线程与进程 concurrent.futures模块

    https://docs.python.org/3/library/concurrent.futures.html 17.4.1 Executor Objects class concurrent.f ...

  5. python concurrent.futures包使用,捕获异常

    concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池.和另外一个第三方threadpool包相比,这个可以非阻塞的 ...

  6. (11)线程池(最新的concurrent.futures包去开启)

    '''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...

  7. Storm/Cassandra集成错误:NoSuchMethodError: concurrent.Futures.withFallback

    本文原文出处: http://blog.csdn.net/bluishglc/article/details/50443205 严禁不论什么形式的转载.否则将托付CSDN官方维护权益. 2015年的最 ...

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

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

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

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

随机推荐

  1. python __getattr__ & __getattribute__ 学习

    实例属性的获取和拦截, 仅对实例属性(instance, variable)有效, 非类属性 getattr: 适用于未定义的属性, 即该属性在实例中以及对应的类的基类以及祖先类中都不存在 1. 动态 ...

  2. Haproxy 让后端RS记录真实IP

    一.修改haproxy.cfg配置文件,在defaults中加入如下两行,并重启haproxy. vim /etc/haproxy/haproxy.cfg defaults option http-s ...

  3. 《剑指Offer》-005 -用两个栈实现队列

    如题 (总结要点) 用两个栈实现队列 栈; 先进后出 队列: 先进先出 两个栈, 相等于两个杯子; 把一本水倒来倒去, 取到杯子底部的元素,并且删除,再倒回去 原文链接 : 借鉴学习文章列表 链接1: ...

  4. ARTS-week8

    Algorithm 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将这两个数相加起来,则会返回一个新的 ...

  5. php怎样应对高并发

    高并发下的数据安全 我们知道在多线程写入同一个文件的时候,会出现“线程安全”的问题(多个线程同时运行同一段代码,如果每次运行结果和单线程运行的结果是一样的,结果和预期相同,就是线程安全的). 如果是M ...

  6. python 使用 requests 做 http 请求

    1. get import requests # 最简单的get请求 r = requests.get(url) print(r.status_code) print(r.json()) # url ...

  7. DOM4j XML 工具类

    之前项目有跟客户系统对接一个webservice系统,该接口有传参和返回都是xml,所以找时间百度研究了一下dom4j,dom4j是一个Java的XML API,是jdom的升级品,用来读写XML文件 ...

  8. 第3章 常用linux命令 3.5 文件压缩命令

    实验六 文件及目录的压缩解压缩相关命令的使用 [实验目的] 1.掌握linux压缩文件实质 2.掌握linux中压缩及解压缩指令的用法 [实验环境] 1. 标准配置PC一台 2. linux操作系统: ...

  9. 关于Java锁(学习笔记)

    个人学习笔记! 1)分布式锁的实现?①数据库实现单点.非重入.非阻塞.无失效时间.依赖数据库(要自己设置,可结合排它锁.乐观锁.悲观锁等混合使用)②缓存(Redis等)集群部署解决单点问题.分布式锁方 ...

  10. (知识点4)C++ 中vector

    1.定义vector<vector<int>> M; 2.添加元素这里是vector的嵌套使用,本质是vector元素里的每个元素也是vector类型,所以抓住本质来添加元素就 ...