测试线程发送。防止阻塞

This commit is contained in:
liuwei
2025-05-30 09:10:30 +08:00
parent 32edc90076
commit d90f02191c

View File

@@ -11,13 +11,10 @@ def send_message_in_thread(func, *args, **kwargs):
"""在独立线程中发送消息""" """在独立线程中发送消息"""
def run(): def run():
try: try:
loop = asyncio.new_event_loop() # 在新线程中运行异步函数
asyncio.set_event_loop(loop) asyncio.run(func(*args, **kwargs))
loop.run_until_complete(func(*args, **kwargs))
except Exception as e: except Exception as e:
logger.error(f"发送消息失败: {e}") logger.error(f"发送消息失败: {e}")
finally:
loop.close()
# 创建并启动线程 # 创建并启动线程
thread = threading.Thread(target=run) thread = threading.Thread(target=run)