测试线程发送。防止阻塞

This commit is contained in:
liuwei
2025-05-30 09:20:11 +08:00
parent 3e4b678a87
commit 280b59a662

View File

@@ -16,8 +16,25 @@ def send_message_in_thread(func, *args, **kwargs):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# 运行异步函数
loop.run_until_complete(func(*args, **kwargs))
# 创建异步任务
async def send():
try:
await func(*args, **kwargs)
finally:
# 发送完成后停止事件循环
loop.stop()
# 创建任务
task = asyncio.create_task(send())
# 运行事件循环直到停止
loop.run_forever()
# 等待任务完成
if not task.done():
task.cancel()
loop.run_until_complete(task)
except Exception as e:
logger.error(f"发送消息失败: {e}")
finally: