feat: 优化整体项目

This commit is contained in:
2025-12-05 18:06:13 +08:00
parent b4df26f61d
commit 7d3ef70093
13 changed files with 2661 additions and 305 deletions

View File

@@ -126,6 +126,59 @@ def add_callback_handler(callback_handler):
logger.debug(f"注册断开回调: {name}")
def remove_callback_handler(callback_handler):
"""
移除回调处理器实例(修复内存泄漏)
Args:
callback_handler: 包含回调方法的对象
"""
global _GLOBAL_CONNECT_CALLBACK_LIST, _GLOBAL_RECV_CALLBACK_LIST, _GLOBAL_CLOSE_CALLBACK_LIST
# 移除属于该处理器的所有回调
_GLOBAL_CONNECT_CALLBACK_LIST = [
f for f in _GLOBAL_CONNECT_CALLBACK_LIST
if not (hasattr(f, '__self__') and f.__self__ is callback_handler)
]
_GLOBAL_RECV_CALLBACK_LIST = [
f for f in _GLOBAL_RECV_CALLBACK_LIST
if not (hasattr(f, '__self__') and f.__self__ is callback_handler)
]
_GLOBAL_CLOSE_CALLBACK_LIST = [
f for f in _GLOBAL_CLOSE_CALLBACK_LIST
if not (hasattr(f, '__self__') and f.__self__ is callback_handler)
]
logger.debug(f"已移除回调处理器: {type(callback_handler).__name__}")
def clear_all_callbacks():
"""
清空所有回调(用于关闭时清理)
"""
global _GLOBAL_CONNECT_CALLBACK_LIST, _GLOBAL_RECV_CALLBACK_LIST, _GLOBAL_CLOSE_CALLBACK_LIST
_GLOBAL_CONNECT_CALLBACK_LIST.clear()
_GLOBAL_RECV_CALLBACK_LIST.clear()
_GLOBAL_CLOSE_CALLBACK_LIST.clear()
logger.debug("已清空所有回调")
def get_callback_count() -> dict:
"""
获取回调数量统计
Returns:
回调数量字典
"""
return {
"connect": len(_GLOBAL_CONNECT_CALLBACK_LIST),
"recv": len(_GLOBAL_RECV_CALLBACK_LIST),
"close": len(_GLOBAL_CLOSE_CALLBACK_LIST)
}
@WINFUNCTYPE(None, ctypes.c_void_p)
def wechat_connect_callback(client_id):
"""