使用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模块(进程池/线程池)
需要注意一下不能无限的开进程,不能无限的开线程最常用的就是开进程池,开线程池.其中回调函数非常重要回调函数其实可以作为一种编程思想,谁好了谁就去掉 只要你用并发,就会有锁的问题,但是你不能一直去自己加 ...
随机推荐
- linux设备驱动程序-i2c(2)-adapter和设备树的解析
linux设备驱动程序-i2c(2)-adapter和设备树的解析 (注: 基于beagle bone green开发板,linux4.14内核版本) 在本系列linux内核i2c框架的前两篇,分别讲 ...
- Python线程join和setDaemon
看一下线程的setDaemon()方法 import time import threading import ctypes import inspect def sayHello(): for i ...
- centos设置开机自启动脚本
1.新建脚本文件 我这里是为了设置开机自动设置ipv6隧道,所以命名为ipv6tunnel.sh ifconfig sit0 up ifconfig sit0 inet6 tunnel ::66.22 ...
- 项目Beta冲刺(团队)——总结篇
项目Beta冲刺(团队)--总结篇 格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺(团队) 团队名称:为了交项目干杯 作业目标:Beta冲刺总结 团队信息 队员学号 ...
- 基线baseline
vertical:元素的垂直对齐方式:默认 vertical:baseline ,一行内对其基线,如下图就是以父元素基线对齐,上图vertical:top 垂直对齐方式,行内元素头部对齐.
- Gamification and Game-Based Learning
https://uwaterloo.ca/centre-for-teaching-excellence/teaching-resources/teaching-tips/educational-tec ...
- react native 开发问题分类
链接.编译.部署.运行: 一.语法问题:编译问题 二.链接问题: yarn add react-navigation react-native link react-navigation 三.部署问题 ...
- [POJ3468]关于整数的简单题 (你想要的)树状数组区间修改区间查询
#include <cstdio> #include <algorithm> #include <cstring> #include <cctype> ...
- Linked List Sorting
静态链表(用结构体数组模拟链表) 1052 Linked List Sorting (25分) A linked list consists of a series of structur ...
- UFUN函数 UF_CSYS函数 UF_MTX函数(如何创建坐标系);
// (题目不够长,写在这了) // 函数有 // UF_MTX3_initialize,UF_CSYS_create_matrix,UF_CSYS_create_csys,UF_CSYS_ask_c ...