feat: 优化整体项目
This commit is contained in:
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user