From 67382a0c0ae07225645c3a5f1dcc6026c9ac45e2 Mon Sep 17 00:00:00 2001 From: passthem Date: Sun, 19 Oct 2025 20:27:18 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E6=88=91=E5=86=99=E7=9A=84=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E9=87=87=E7=94=A8=E6=9B=B4=E5=AE=89=E5=85=A8=E7=9A=84?= =?UTF-8?q?=20asyncio=20=E9=94=81=E5=86=99=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- konabot/common/longtask.py | 9 ++-- konabot/plugins/simple_notify/__init__.py | 63 +++++++++++++---------- 2 files changed, 41 insertions(+), 31 deletions(-) diff --git a/konabot/common/longtask.py b/konabot/common/longtask.py index 618317a..abc83c2 100644 --- a/konabot/common/longtask.py +++ b/konabot/common/longtask.py @@ -254,11 +254,10 @@ def _save_longtask_data(data: LongTaskModuleData): @asynccontextmanager async def longtask_data(): - await longtask_lock.acquire() - data = _load_longtask_data() - yield data - _save_longtask_data(data) - longtask_lock.release() + async with longtask_lock: + data = _load_longtask_data() + yield data + _save_longtask_data(data) async def create_longtask( diff --git a/konabot/plugins/simple_notify/__init__.py b/konabot/plugins/simple_notify/__init__.py index 8f0b5d3..6cd53b5 100644 --- a/konabot/plugins/simple_notify/__init__.py +++ b/konabot/plugins/simple_notify/__init__.py @@ -15,10 +15,10 @@ from nonebot.adapters.console.event import MessageEvent as ConsoleMessageEvent from nonebot.adapters.discord import Bot as DiscordBot from nonebot.adapters.discord.event import MessageEvent as DiscordMessageEvent from nonebot.adapters.onebot.v11 import Bot as OnebotV11Bot -from nonebot.adapters.onebot.v11.event import \ - GroupMessageEvent as OnebotV11GroupMessageEvent -from nonebot.adapters.onebot.v11.event import \ - MessageEvent as OnebotV11MessageEvent +from nonebot.adapters.onebot.v11.event import ( + GroupMessageEvent as OnebotV11GroupMessageEvent, +) +from nonebot.adapters.onebot.v11.event import MessageEvent as OnebotV11MessageEvent from nonebot_plugin_alconna import UniMessage, UniMsg from pydantic import BaseModel @@ -68,14 +68,14 @@ def save_notify_config(config: NotifyConfigFile): async def notify_now(notify: Notify): - if notify.platform == 'console': + if notify.platform == "console": bot = [b for b in nonebot.get_bots().values() if isinstance(b, ConsoleBot)] if len(bot) != 1: logger.warning(f"提醒未成功发送出去:{nonebot.get_bots()} {notify}") return False bot = bot[0] await bot.send_private_message(notify.target, f"代办通知:{notify.notify_msg}") - elif notify.platform == 'discord': + elif notify.platform == "discord": bot = [b for b in nonebot.get_bots().values() if isinstance(b, DiscordBot)] if len(bot) != 1: logger.warning(f"提醒未成功发送出去:{nonebot.get_bots()} {notify}") @@ -83,7 +83,7 @@ async def notify_now(notify: Notify): bot = bot[0] channel = await bot.create_DM(recipient_id=int(notify.target)) await bot.send_to(channel.id, f"代办通知:{notify.notify_msg}") - elif notify.platform == 'qq': + elif notify.platform == "qq": bot = [b for b in nonebot.get_bots().values() if isinstance(b, OnebotV11Bot)] if len(bot) != 1: logger.warning(f"提醒未成功发送出去:{nonebot.get_bots()} {notify}") @@ -92,17 +92,22 @@ async def notify_now(notify: Notify): if notify.target_env is None: await bot.send_private_msg( user_id=int(notify.target), - message=cast(Any, await UniMessage.text(f"代办通知:{notify.notify_msg}").export( - bot=bot, - )), + message=cast( + Any, + await UniMessage.text(f"代办通知:{notify.notify_msg}").export( + bot=bot, + ), + ), ) else: await bot.send_group_msg( group_id=int(notify.target_env), - message=cast(Any, - await UniMessage().at( - notify.target - ).text(f" 代办通知:{notify.notify_msg}").export(bot=bot) + message=cast( + Any, + await UniMessage() + .at(notify.target) + .text(f" 代办通知:{notify.notify_msg}") + .export(bot=bot), ), ) else: @@ -127,15 +132,17 @@ def create_notify_task(notify: Notify, fail2remove: bool = True): ) res = await notify_now(notify) if fail2remove or res: - await DATA_FILE_LOCK.acquire() - cfg = load_notify_config() - cfg.notifies = [n for n in cfg.notifies if n.get_str() != notify.get_str()] - if not res: - cfg.unsent.append(notify) - save_notify_config(cfg) - DATA_FILE_LOCK.release() + async with DATA_FILE_LOCK: + cfg = load_notify_config() + cfg.notifies = [ + n for n in cfg.notifies if n.get_str() != notify.get_str() + ] + if not res: + cfg.unsent.append(notify) + save_notify_config(cfg) else: pass + return asynkio.create_task(mission()) @@ -201,8 +208,12 @@ async def _(msg: UniMsg, mEvt: Event): save_notify_config(cfg) DATA_FILE_LOCK.release() - await evt.send(await UniMessage().at(mEvt.get_user_id()).text( - f" 了解啦!将会在 {notify.notify_time} 提醒你哦~").export()) + await evt.send( + await UniMessage() + .at(mEvt.get_user_id()) + .text(f" 了解啦!将会在 {notify.notify_time} 提醒你哦~") + .export() + ) logger.info(f"创建了一条于 {notify.notify_time} 的代办提醒") @@ -253,8 +264,8 @@ async def _(): logger.info("所有的代办提醒 Task 都已经退出了") for sig in (signal.SIGINT, signal.SIGTERM): - loop.add_signal_handler(sig, functools.partial( - asynkio.create_task, shutdown(sig) - )) + loop.add_signal_handler( + sig, functools.partial(asynkio.create_task, shutdown(sig)) + ) await asynkio.gather(*ASYNK_TASKS)