適配成同一個一樣的公有方法。app
# -*- 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)