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

使并发核心池能够在 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. 【VNCserver】Centos7.4安装VNC连接华为云或亚马逊云

    1.1 文档背景 CentOS 7 / RHEL 7部署图形化界面 安装VNCserver实现linux系统云主机桌面化,通过普通用户实现桌面化操作 2.  Vncserver服务端部署 2.1 安装 ...

  2. 在visual studio中找到属性管理器

    方法一: 我们可以在新建的项目中通过视图-->其他窗口-->属性管理器来找到. 这样就能找到属性管理器了. 感谢:https://blog.csdn.net/qq_37939434/art ...

  3. 执行shell脚本时提示/bin/sh^M: bad interpreter: No such file or directory

    执行脚本时提示解释器有问题,错误提示如下: 这种提示一般是脚本在windows系统之通过记事本写的,记事本修改过的文本,会默认在文本前面加上一些看不到的标记,导致shell脚本不能被shell解释器识 ...

  4. 洛谷 P3071 [USACO13JAN]座位Seating(线段树)

    P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...

  5. docker学习7-Dockerfile制作自己的镜像文件

    前言 如果你是一个python自动化测试人员,某天你在公司终于完成了一个项目的接口自动化脚本工作,在你自己常用的本机或者服务器上调试完成了脚本,稳稳地没问题. 可是晚上下班回家,你自己找了个linux ...

  6. js 正则表达式2

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

  7. 缺jar包异常:java.lang.NoClassDefFoundError: org/springframework/core/convert/support/PropertyTypeDescriptor

    严重: StandardWrapper.Throwable java.lang.NoClassDefFoundError: org/springframework/core/convert/suppo ...

  8. JUnit 4.x 与 5.x 的区别?

    区别项 4.x 5.x 手动把测试和测试方法声明为public 需要 不需要 @Test 与JUnit 4的@Test注解不同的是,它没有声明任何属性,因为JUnit Jupiter中的测试扩展是基于 ...

  9. STL str replace

    #include <iostream> #include <string> using namespace std; void main() { string s=" ...

  10. ent 基本使用六 Mixin

    ent 的Mixin 可以让我们服用已有的schema Mixin 接口说明 type Mixin interface { Fields() []ent.Field } 一个demo 代码 // -- ...