类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类。

使并发核心池能够在 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 相同的公有方法。组成鸭子类的更多相关文章

  1. 使用evenlet包实现 concurrent.futures.executor包的鸭子类

    适配成同一个同样的公有方法. # -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/7/3 10:35 import time import w ...

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

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

  3. concurrent.futures

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

  4. concurrent.futures模块简单介绍(线程池,进程池)

    一.基类Executor Executor类是ThreadPoolExecutor 和ProcessPoolExecutor 的基类.它为我们提供了如下方法: submit(fn, *args, ** ...

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

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

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

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

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

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

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

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

  9. concurrent.futures模块(进程池/线程池)

    需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...

随机推荐

  1. 【转】tf.train.MonitoredTrainingSession()解析

    原文地址:https://blog.csdn.net/mrr1ght/article/details/81006343. 本文有删减. MonitoredTrainingSession定义 首先,tf ...

  2. python与设计模式--单例模式

    https://zhuanlan.zhihu.com/p/31675841 设计模式分类 创建类 单例模式.工厂模式.抽象工厂模式.原型模式.建造者模式 结构类 装饰器模式.适配器模式.门面模式.组合 ...

  3. JMeter的执行顺序

    JMeter执行顺序如下:1.配置元件2.前置处理器3.定时器4.采样器5.后置处理器(除非服务器响应为空)6.断言(除非服务器响应为空)7.监听器(除非服务器响应为空) 只有当作用域内存在采样器时, ...

  4. js 正则表达式2

    对于某些特殊的字符,我们 必须转义一下才可以使用.(注意一点,我们使用那些需要转义的字符是通过"\"+相应的字符来构成的,记住是"\",而不也是"/& ...

  5. M × N Puzzle

    http://poj.org/problem?id=2893 来自逆序对的强大力量 #include<iostream> #include<stdio.h> #include& ...

  6. 2019牛客暑期多校训练营(第八场)A:All-one Matrices(广告牌问题 单调队列)

    题意:给出N*M的01矩阵,求矩阵个数,满足矩阵内全是‘1’,,而且被至少一个’0‘围住.(假设边界外是‘0’.(N,M<3000) 思路:这类问题,一般解决就是两个方向: A:压缩一维,即枚举 ...

  7. 【Spark】ScalaIDE运行spark,A master URL must be set in your configuration

    or SparkSession.master("local")

  8. Go语言 - 接口

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

  9. React.js 小书

    http://huziketang.mangojuice.top/books/react/

  10. 请找出"aaaabbcccdddd"字符串中出现最多的字母

    function getCount(str) { for(var code=32;code<128;code++){ var mych=String.fromCharCode(code); va ...