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

# -*- 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. mysql分组统计按照字段排序方法(分组之后保留最新时间、最大id...)

    sql示例如下: select success_time,query_time,order_no from pro_return_plan t where t.success_time in ( SE ...

  2. 【Linux】linux ln文件夹的链接(转)

    1 文件的链接ln -s a.py b.pya.py 软链接到b.py (无论 b.py 文件存在与否 都可以成功)2 文件夹的链接ln -s a ba文件夹软链接到b文件夹(b文件夹不能提前创建) ...

  3. hive删除数据(转)

    转自:https://www.cnblogs.com/linn/p/6196293.html 按分区删除: ALTER TABLE test1  DROP PARTITION (dt='2016-04 ...

  4. hive的常用函数工作总结

    1.concat_ws 它是一个特殊形式的 CONCAT() concat_ws(分隔符,参数1,参数2.......) as 字段 2.split 返回值为一个数组 a.基本用法: 例1:split ...

  5. tasklist /m

    \>tasklist /m explorer*映像名称                       PID 模块                                        = ...

  6. 关于python Tk中实时的输出.

    源码如下: import time from Tkinter import * def run(): while True: txt.insert(END,'...') print '...' tim ...

  7. Fiddler实现篡改接口请求和返回数据

    步骤如下: 点击rules->Automatic Breakpoints,在这个选项下,我们可以看到三个可选项: Before Requests:在请求发出前拦截请求: After Reques ...

  8. Chrome插件安装和用法

    XPath Helper 下载插件,拖入chrome://extensions/ 使用方法:ctrl+shift+x呼出 JSONView的使用: 安装JSONView插件 下载插件,拖入chrome ...

  9. Go语言 - 接口

    接口类型 在Go语言中接口(interface)是一种类型,一种抽象的类型. interface是一组method的集合,是duck-type programming的一种体现.接口做的事情就像是定义 ...

  10. MySQL中列别名为中文时,Order by 子句中使用别名时不要加引号

    暂时还不清楚原因 1.按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩 SC表: 这里,当做总成绩处理 select sid, sum(score) as '总成绩', avg(score) ...