fix module 'eventlet.hubs.hub' has no attribute 'Hub' 错误
This commit is contained in:
@@ -10,17 +10,18 @@ eventlet.monkey_patch()
|
||||
|
||||
# 配置 eventlet hub 以更优雅地处理所有错误
|
||||
try:
|
||||
from eventlet.hubs import hub
|
||||
from eventlet import hubs
|
||||
import eventlet
|
||||
import traceback
|
||||
|
||||
# 获取当前的 hub 实例
|
||||
current_hub = hubs.get_hub()
|
||||
|
||||
# 保存原始的方法
|
||||
original_handle_error = hub.Hub.handle_error
|
||||
original_switch = hubs.hub.Hub.switch
|
||||
original_fire_timers = hubs.hub.Hub.fire_timers
|
||||
original_handle_error = hubs.handle_error
|
||||
original_trampoline = hubs.trampoline
|
||||
|
||||
def custom_handle_error(self, context, type, value, tb):
|
||||
def custom_handle_error(context, type, value, tb):
|
||||
# 捕获并打印所有 eventlet.hubs 相关错误,不再往外raise
|
||||
print(f"[Eventlet Hub Error] Type: {type.__name__}, Value: {value}")
|
||||
print(f"[Context] {context}")
|
||||
@@ -28,28 +29,6 @@ try:
|
||||
traceback.print_tb(tb)
|
||||
# 不再调用原始方法,直接返回,不往外传播错误
|
||||
|
||||
def custom_switch(self):
|
||||
try:
|
||||
return original_switch(self)
|
||||
except Exception as e:
|
||||
# 捕获 switch 方法中所有异常
|
||||
print(f"[Eventlet Switch Error] {type(e).__name__}: {e}")
|
||||
print("[Traceback]")
|
||||
traceback.print_exc()
|
||||
# 返回 None 继续运行,不往外传播错误
|
||||
return None
|
||||
|
||||
def custom_fire_timers(self):
|
||||
try:
|
||||
return original_fire_timers(self)
|
||||
except Exception as e:
|
||||
# 捕获 fire_timers 方法中所有异常
|
||||
print(f"[Eventlet Fire Timers Error] {type(e).__name__}: {e}")
|
||||
print("[Traceback]")
|
||||
traceback.print_exc()
|
||||
# 返回继续运行,不往外传播错误
|
||||
return
|
||||
|
||||
def custom_trampoline(fd, read=False, write=False, timeout=None, timeout_exc=None):
|
||||
try:
|
||||
return original_trampoline(fd, read, write, timeout, timeout_exc)
|
||||
@@ -63,9 +42,7 @@ try:
|
||||
return None
|
||||
|
||||
# 替换原始方法
|
||||
hub.Hub.handle_error = custom_handle_error
|
||||
hubs.hub.Hub.switch = custom_switch
|
||||
hubs.hub.Hub.fire_timers = custom_fire_timers
|
||||
hubs.handle_error = custom_handle_error
|
||||
hubs.trampoline = custom_trampoline
|
||||
|
||||
# 同样处理 greenio 模块的所有相关方法,捕获所有异常
|
||||
@@ -153,31 +130,6 @@ try:
|
||||
# 如果修改失败,忽略错误
|
||||
print(f"[WSGI Patch Error] {e}")
|
||||
|
||||
# 修改 eventlet 所有可能抛出异常的核心方法
|
||||
# 遍历 hub.Hub 类的所有方法,包装可能抛出异常的方法
|
||||
for method_name in dir(hub.Hub):
|
||||
if not method_name.startswith('_'):
|
||||
method = getattr(hub.Hub, method_name)
|
||||
if callable(method):
|
||||
def wrap_method(original_method):
|
||||
def wrapped_method(self, *args, **kwargs):
|
||||
try:
|
||||
return original_method(self, *args, **kwargs)
|
||||
except Exception as e:
|
||||
print(f"[Eventlet Hub Method Error] {method_name}: {type(e).__name__}: {e}")
|
||||
print("[Traceback]")
|
||||
traceback.print_exc()
|
||||
# 返回适当的默认值
|
||||
if method_name == 'add':
|
||||
return None
|
||||
elif method_name == 'remove':
|
||||
return None
|
||||
return None
|
||||
return wrapped_method
|
||||
|
||||
# 替换原始方法
|
||||
setattr(hub.Hub, method_name, wrap_method(method))
|
||||
|
||||
print("[Eventlet Error Handling] All eventlet.hubs errors will be caught and printed, not raised")
|
||||
except Exception as e:
|
||||
# 如果修改失败,打印错误但继续运行
|
||||
|
||||
Reference in New Issue
Block a user