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

使并发核心池能够在 threadpoolexetor和geventpoolexecutor自由选一种切换。

实现方式。

  1. # -*- coding: utf-8 -*-
  2. # @Author : ydf
  3. # @Time : 2019/7/2 14:11
  4. import atexit
  5. import time
  6. import warnings
  7. from collections import Callable
  8.  
  9. import gevent
  10. from gevent import pool as gevent_pool
  11. from gevent import monkey
  12.  
  13. from gevent.queue import JoinableQueue
  14.  
  15. from app.utils_ydf import LoggerMixin, nb_print, LogManager
  16.  
  17. def check_gevent_monkey_patch(raise_exc=True):
  18. if not monkey.is_module_patched('socket'):
  19. if raise_exc:
  20. warnings.warn(f'检测到 你还没有打gevent包的猴子补丁,请在所运行的脚本第一行写上 【import gevent.monkey;gevent.monkey.patch_all()】 这句话。')
  21. raise Exception(f'检测到 你还没有打gevent包的猴子补丁,请在所运行的脚本第一行写上 【import gevent.monkey;gevent.monkey.patch_all()】 这句话。')
  22. else:
  23. return 1
  24.  
  25. logger_gevent_timeout_deco = LogManager('logger_gevent_timeout_deco').get_logger_and_add_handlers()
  26.  
  27. def gevent_timeout_deco(timeout_t):
  28. def _gevent_timeout_deco(f):
  29. def __gevent_timeout_deceo(*args, **kwargs):
  30. timeout = gevent.Timeout(timeout_t, )
  31. timeout.start()
  32. try:
  33. f(*args, **kwargs)
  34. except gevent.Timeout as t:
  35. logger_gevent_timeout_deco.error(f'函数 {f} 运行超过了 {timeout_t} 秒')
  36. if t is not timeout:
  37. nb_print(t)
  38. # raise # not my timeout
  39. finally:
  40. timeout.close()
  41.  
  42. return __gevent_timeout_deceo
  43.  
  44. return _gevent_timeout_deco
  45.  
  46. class GeventPoolExecutor(gevent_pool.Pool):
  47. def __init__(self, size=None, ):
  48. check_gevent_monkey_patch()
  49. super().__init__(size, )
  50.  
  51. def submit(self, *args, **kwargs):
  52. self.spawn(*args, **kwargs)
  53.  
  54. def shutdown(self):
  55. self.join()
  56.  
  57. if __name__ == '__main__':
  58. monkey.patch_all()
  59.  
  60. def f2(x):
  61.  
  62. time.sleep(1)
  63. nb_print(x)
  64.  
  65. pool = GeventPoolExecutor(4)
  66.  
  67. for i in range(15):
  68. nb_print(f'放入{i}')
  69. pool.submit(gevent_timeout_deco(8)(f2), i)
  70. 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. 记录一次win2003服务器的IIS服务加载.flv后缀的资源报错404的处理方法

    问题:访问某个域名下的xxxx.flv资源,页面报错404. 解决思路: 1.权限是否给足 user权限给完全控制咯 如果你访问该域名下的其他资源无问题的话就不是介个原因了 2.MIME类型是否少了 ...

  2. C#控制多线程最大并行数量

  3. BitMap 图片格式与Base64Image格式互转方法

    BitMap 图片格式与Base64Image格式互转方法 /// <summary> /// 图片转为base64编码的字符串 /// </summary> /// < ...

  4. destoon6.0 手机版添加下载模块

    common.inc.php  里面加入 下载板块的down 名称 在include 文件里 加入 down.inc.php 文件 在 mobile模版里 加入 down.htm 模版文件 在 电脑版 ...

  5. SpringBoot——SpringBoot学习记录【一】

    前言 公司目前主要的业务,用的语言是java,所以学习下相关的技术呀,还好大学基础语言学的JAVA SpringBoot简介 官网 SpringBoot 简介 SpringBoot是用来简化Sprin ...

  6. Ofbiz项目学习——阶段性小结——查询

    一.组装参数的学习 首先是查询条件,对于查询条件,需要判断是否从前端传递空值?——怎么处理查询空值? 当然可以一个一个进行判断,但是这样代码会导致很多,可以统一处理,形成一个公共方法. 1. 单个处理 ...

  7. redux沉思录

    要素:store.reducer.dispatch/subscribe connect:将业务逻辑剥离到容器类,数据的双向绑定: 数据.操作.UI分离.命令封装 核心思想:对共享状态的维护: 核心代码 ...

  8. RxSwift 在本质上简化了开发异步程序

    RxSwift 是一个组合异步和事件驱动编程的库,通过使用可观察序列和功能样式运算符来,从而允许通过调度程序进行参数化执行. RxSwift 在本质上简化了开发异步程序,允许代码对新数据作出反应,并以 ...

  9. linux学习13 Linux运维常用文件管理命令及系统变量基础

    一.文件管理命令 1.cp命令,copy a.单源复制,cp [OPTION]... [-T] SOURCE DEST 如果DEST不存在:则事先创建此文件,并复制源文件的数据流至DEST中. 如果D ...

  10. 使用google autoservice 自动生成java spi 描述文件

    spi 是一种服务发现的标准,对于开发中我们通常需要编写 META-INF/services 文件夹中定义的类. google auto 中的autoservice 可以帮助我们生成对应的配置,很方便 ...