使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类
类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类。
使并发核心池能够在 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 相同的公有方法。组成鸭子类的更多相关文章
- 使用evenlet包实现 concurrent.futures.executor包的鸭子类
适配成同一个同样的公有方法. # -*- coding: utf-8 -*- # @Author : ydf # @Time : 2019/7/3 10:35 import time import w ...
- python异步并发模块concurrent.futures入门详解
concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- concurrent.futures模块简单介绍(线程池,进程池)
一.基类Executor Executor类是ThreadPoolExecutor 和ProcessPoolExecutor 的基类.它为我们提供了如下方法: submit(fn, *args, ** ...
- 线程与进程 concurrent.futures模块
https://docs.python.org/3/library/concurrent.futures.html 17.4.1 Executor Objects class concurrent.f ...
- python concurrent.futures包使用,捕获异常
concurrent.futures的ThreadPoolExecutor类暴露的api很好用,threading模块抹油提供官方的线程池.和另外一个第三方threadpool包相比,这个可以非阻塞的 ...
- (11)线程池(最新的concurrent.futures包去开启)
'''concurrent.futures是最新的开启线程池的包'''import timefrom concurrent.futures import ThreadPoolExecutor #开启线 ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- concurrent.futures模块(进程池/线程池)
需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...
随机推荐
- 树莓派无显示屏连接wifi
在烧好Raspbian系统的TF卡boot分区新建 wpa_supplicant.conf 文件,内容如下(修改自己的WIFI名和密码,key_mgmt根据路由器配置),保存后启动树莓派即可自动连接W ...
- linux lvm管理基础教程
linux lvm管理基础教程 本人是在redhat7.x系统上亲测lvm管理功能,至于文中所受的CentOS 6 没有亲自试过. 本文来自:https://geekpeek.net/lvm-phys ...
- 洛谷P1084 疫情控制(贪心+倍增)
这个题以前写过一遍,现在再来写,感觉以前感觉特别不好写的细节现在好些多了,还是有进步吧. 这个题的核心思想就是贪心+二分.因为要求最小时间,直接来求问题将会变得十分麻烦,但是如果转换为二分答案来判断可 ...
- blockingqueue.h
#include <mutex> #include <condition_variable> #include <deque> template <typen ...
- 用户体验报告(Echo)
班级:软件工程1916|W 作业:项目Beta冲刺(团队) 团队名称:Echo 团队博客汇总 队员学号 队员姓名 个人博客地址 备注 221600136 张至锋 https://www.cnblogs ...
- Spring Cloud Task 知识点
Spring Cloud Task的目标是为Spring Boot应用程序提供创建短期运行微服务的功能. 出处:https://blog.csdn.net/peterwanghao/article/d ...
- scala简单的功能实现~weektwo
1.编写⼀个BankAccount类,假如deposit和withdraw⽅法,和⼀个只读的balance属性. //存款(deposit)和取款()函数 class BankAccount exte ...
- redux有价值的文档
使用 Redux 管理状态,第 1 部分 https://www.ibm.com/developerworks/cn/web/wa-manage-state-with-redux-p1-david-g ...
- day004-python运算符与基本数据类型
一.运算符1.算术运算符:主要用于两个对象算数计算(加减乘除等运算)运算符: +:两个对象相加 -:得到负数或是一个数减去另一个数 *:两个数相乘或是返回一个被重复若干次的字符串 /:x除以y %:返 ...
- ID 迭代加深搜索 模板 埃及分数
#include <bits/stdc++.h> using namespace std; #define LL long long int Maxd; LL Ans[10], now[1 ...