使用evenlet包实现 concurrent.futures.executor包的鸭子类
适配成同一个同样的公有方法。
# -*- 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包的鸭子类的更多相关文章
- 使用gevent包实现concurrent.futures.executor 相同的公有方法。组成鸭子类
类名不同,但公有方法的名字和提供的基本功能大致相同,但两个类没有共同继承的祖先或者抽象类 接口来规定他,叫鸭子类. 使并发核心池能够在 threadpoolexetor和geventpoolexecu ...
- python异步并发模块concurrent.futures入门详解
concurrent.futures是一个非常简单易用的库,主要用来实现多线程和多进程的异步并发. 本文主要对concurrent.futures库相关模块进行详解,并分别提供了详细的示例demo. ...
- concurrent.futures
concurrent.futures concurrent.futures提供高层次的接口,用来实现异步调用. 这个异步执行可以使用threads(ThreadPoolExecutor)或者proce ...
- 线程与进程 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 #开启线 ...
- Storm/Cassandra集成错误:NoSuchMethodError: concurrent.Futures.withFallback
本文原文出处: http://blog.csdn.net/bluishglc/article/details/50443205 严禁不论什么形式的转载.否则将托付CSDN官方维护权益. 2015年的最 ...
- 线程池、进程池(concurrent.futures模块)和协程
一.线程池 1.concurrent.futures模块 介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 Pro ...
- Python标准模块--concurrent.futures
1 模块简介 concurrent.futures模块是在Python3.2中添加的.根据Python的官方文档,concurrent.futures模块提供给开发者一个执行异步调用的高级接口.con ...
随机推荐
- 【RabbitMQ】RabbitMQ的安装以及基本概念的介绍
一.如何安装 https://www.cnblogs.com/756623607-zhang/p/11469962.html 二.基本概念介绍 ·Broker:可以理解为消息队列服务器的实体,它是一个 ...
- Linux centos关机和重启命令
Linux centos重启命令: 1.reboot 普通重启 2.shutdown -r now 立刻重启(root用户使用) 3.shutdown -r 10 过10分钟自动重启(root用户 ...
- html中的数据岛:利用DSO和javascript在html中动态加载和浏览xml数据
1.DSO也叫做数据源对象,IE 4.0引入了DSO,在IE 5.0对DSO技术进行很大的扩展.以往如果数据是通过SQL语言对数据库进行查询得到的结果,那么就把它们存放在ADO(ActiveX Dat ...
- wordpress下一篇next_post_link函数的使用方法
我们在用wordpress开发时经常会用到上一篇下一篇的功能,<?php previous_post_link('%link') ?> <?php next_post_link('% ...
- strings命令(转)
以前我用strings命令的频率不高, 现在越用越顺手, 而且已经是离不开strings命令了.虽然以前说过strings命令, 但今天还是要说. 主要是两大用途, 下面来说明一下: 一. 确认代码编 ...
- ent 基本使用十三 debug 模式
ent 生成的代码client 包中包含了一个方便的方法Debug(), 记得昨天我为了查看生成的sql 查询通过配置mysql 启用慢查询,同时设置记录慢查询为0,实际上client Debug 方 ...
- Vuejs组件基础
一.概念 ①组件就是对局部视图的封装,组件思想就是把一个很大的复杂的 Web 页面视图给拆分成一块一块的组件视图,然后利用某种特定的方式把它们组织到一起完成完整的 Web 应用构建. ②目前主流的前端 ...
- 分享一个Centos8的国内yum源
使用的是清华大学开源镜像站,文件地址: https://github.com/hackyoMa/docker-centos/blob/8/CentOS-Base.repo 使用方法: cd /etc/ ...
- openjudge1.1
目录 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.1.10 1.1.1 描述 对于大部分编程语言来说,编写一个能够输出"H ...
- 【JZOJ5553】【20190625】谜
题目 给出一个\(2\times n\)个点的二分图的邻接矩阵\(M\) 以及\(m\)个行替换元,\(k\)个列替换元 \(q\)次询问:op u v 表示用第v个行/列替换元去替换矩阵的第u行/列 ...