测试线程发送。防止阻塞
This commit is contained in:
@@ -20,23 +20,17 @@ def send_message_in_thread(func, *args, **kwargs):
|
|||||||
async def send():
|
async def send():
|
||||||
try:
|
try:
|
||||||
await func(*args, **kwargs)
|
await func(*args, **kwargs)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error(f"发送消息失败: {e}")
|
||||||
finally:
|
finally:
|
||||||
# 发送完成后停止事件循环
|
# 发送完成后停止事件循环
|
||||||
loop.stop()
|
loop.stop()
|
||||||
|
|
||||||
# 创建任务
|
# 创建并运行任务,不等待结果
|
||||||
task = asyncio.create_task(send())
|
asyncio.run_coroutine_threadsafe(send(), loop)
|
||||||
|
|
||||||
# 运行事件循环直到停止
|
|
||||||
loop.run_forever()
|
|
||||||
|
|
||||||
# 等待任务完成
|
|
||||||
if not task.done():
|
|
||||||
task.cancel()
|
|
||||||
loop.run_until_complete(task)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"发送消息失败: {e}")
|
logger.error(f"线程执行失败: {e}")
|
||||||
finally:
|
finally:
|
||||||
# 确保清理资源
|
# 确保清理资源
|
||||||
if loop is not None:
|
if loop is not None:
|
||||||
|
|||||||
Reference in New Issue
Block a user