From adc9b76688ba9816b0219de2cc1fdfacafb7f39f Mon Sep 17 00:00:00 2001 From: snowy Date: Fri, 16 Aug 2024 02:56:50 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E4=BF=AE=E5=A4=8D=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E9=87=8D=E5=90=AF=E8=BF=9B=E7=A8=8B=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=EF=BC=88=E6=9C=AA=E5=90=AF=E5=8A=A8=E7=94=9F=E5=91=BD=E7=9B=91?= =?UTF-8?q?=E8=A7=86=E5=99=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- liteyuki/comm/channel.py | 2 +- liteyuki/comm/shared_memory.py | 14 ++++ liteyuki/core/manager.py | 2 + liteyuki/utils.py | 2 + pyproject.toml | 64 +++++++++---------- .../scheduled_tasks/__init__.py | 18 ++++++ .../divided_by_lifespan/__init__.py | 10 +++ .../divided_by_lifespan/after_start.py | 23 +++++++ 8 files changed, 102 insertions(+), 33 deletions(-) create mode 100644 liteyuki/comm/shared_memory.py create mode 100644 src/liteyuki_plugins/scheduled_tasks/__init__.py create mode 100644 src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/__init__.py create mode 100644 src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/after_start.py diff --git a/liteyuki/comm/channel.py b/liteyuki/comm/channel.py index eae83da7..3af074f2 100644 --- a/liteyuki/comm/channel.py +++ b/liteyuki/comm/channel.py @@ -27,7 +27,7 @@ SYNC_FILTER_FUNC: TypeAlias = Callable[[Any], bool] ASYNC_FILTER_FUNC: TypeAlias = Callable[[Any], Awaitable[bool]] FILTER_FUNC: TypeAlias = SYNC_FILTER_FUNC | ASYNC_FILTER_FUNC -IS_MAIN_PROCESS = multiprocessing.current_process().name == "MainProcess" + _channel: dict[str, "Channel"] = {} _callback_funcs: dict[str, ON_RECEIVE_FUNC] = {} diff --git a/liteyuki/comm/shared_memory.py b/liteyuki/comm/shared_memory.py new file mode 100644 index 00000000..1cfc6f67 --- /dev/null +++ b/liteyuki/comm/shared_memory.py @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +""" +共享内存模块。类似于redis,但是更加轻量级。 +""" + +memory_database = {} + + +def set_memory(key: str, value: any) -> None: + pass + + +def get_mem_data(key: str) -> any: + pass diff --git a/liteyuki/core/manager.py b/liteyuki/core/manager.py index 4e3ffef3..f2e3cbca 100644 --- a/liteyuki/core/manager.py +++ b/liteyuki/core/manager.py @@ -87,6 +87,8 @@ class ProcessManager: else: logger.warning("Unknown data received, ignored.") + _start_monitor() + def start_all(self): """ 启动所有进程 diff --git a/liteyuki/utils.py b/liteyuki/utils.py index 73c4987d..785dfd62 100644 --- a/liteyuki/utils.py +++ b/liteyuki/utils.py @@ -4,11 +4,13 @@ """ import asyncio import inspect +import multiprocessing from pathlib import Path from typing import Any, Callable, Coroutine from liteyuki.log import logger +IS_MAIN_PROCESS = multiprocessing.current_process().name == "MainProcess" def is_coroutine_callable(call: Callable[..., Any]) -> bool: """ diff --git a/pyproject.toml b/pyproject.toml index 6af684ac..6e76ab61 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,38 +11,38 @@ license = "MIT & LSO" package-mode = false -[tool.poetry.dependencies] -python = "^3.10" -aiofiles = "~23.2.1" -aiohttp = "~3.9.3" -aiosqlite3 = "~0.3.0" -colored = "~2.2.4" -fastapi = "~0.110.0" -GitPython = "~3.1.42" -httpx = "~0.27.0" -importlib_metadata = "~7.0.2" -jieba = "~0.42.1" -loguru = "~0.7.2" -nb-cli = "~1.4.1" -nonebot-adapter-onebot = "~2.4.3" -nonebot-adapter-satori = "~0.11.5" -nonebot-plugin-alconna = "~0.46.3" -nonebot-plugin-apscheduler = "~0.4.0" -nonebot-plugin-htmlrender = "~0.3.1" -nonebot2 = { version = "~2.3.0", extras = ["fastapi", "httpx", "websockets"] } -numpy = "~2.0.0" -packaging = "~23.1" -psutil = "~5.9.8" -py-cpuinfo = "~9.0.0" -pydantic = "~2.7.0" -Pygments = "~2.17.2" -python-dotenv = "~1.0.1" -pytest = "~8.3.1" -pytz = "~2024.1" -PyYAML = "~6.0.1" -requests = "~2.31.0" -starlette = "~0.36.3" -watchdog = "~4.0.0" +#[tool.poetry.dependencies] +#python = "^3.10" +#aiofiles = "~23.2.1" +#aiohttp = "~3.9.3" +#aiosqlite3 = "~0.3.0" +#colored = "~2.2.4" +#fastapi = "~0.110.0" +#GitPython = "~3.1.42" +#httpx = "~0.27.0" +#importlib_metadata = "~7.0.2" +#jieba = "~0.42.1" +#loguru = "~0.7.2" +#nb-cli = "~1.4.1" +#nonebot-adapter-onebot = "~2.4.3" +#nonebot-adapter-satori = "~0.11.5" +#nonebot-plugin-alconna = "~0.46.3" +#nonebot-plugin-apscheduler = "~0.4.0" +#nonebot-plugin-htmlrender = "~0.3.1" +#nonebot2 = { version = "~2.3.0", extras = ["fastapi", "httpx", "websockets"] } +#numpy = "~2.0.0" +#packaging = "~23.1" +#psutil = "~5.9.8" +#py-cpuinfo = "~9.0.0" +#pydantic = "~2.7.0" +#Pygments = "~2.17.2" +#python-dotenv = "~1.0.1" +#pytest = "~8.3.1" +#pytz = "~2024.1" +#PyYAML = "~6.0.1" +#requests = "~2.31.0" +#starlette = "~0.36.3" +#watchdog = "~4.0.0" [[tool.poetry.source]] diff --git a/src/liteyuki_plugins/scheduled_tasks/__init__.py b/src/liteyuki_plugins/scheduled_tasks/__init__.py new file mode 100644 index 00000000..f8d0c15d --- /dev/null +++ b/src/liteyuki_plugins/scheduled_tasks/__init__.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +""" +Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved + +@Time : 2024/8/15 下午11:29 +@Author : snowykami +@Email : snowykami@outlook.com +@File : __init__.py.py +@Software: PyCharm +""" +from liteyuki.plugin import PluginMetadata + +from .divided_by_lifespan import * + +__plugin_mata__ = PluginMetadata( + name="计划任务", + description="计划任务插件,一些杂项任务的计划执行。", +) diff --git a/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/__init__.py b/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/__init__.py new file mode 100644 index 00000000..8f9b0e0a --- /dev/null +++ b/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/__init__.py @@ -0,0 +1,10 @@ +# -*- coding: utf-8 -*- +""" +Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved + +@Time : 2024/8/15 下午11:32 +@Author : snowykami +@Email : snowykami@outlook.com +@File : __init__.py +@Software: PyCharm +""" \ No newline at end of file diff --git a/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/after_start.py b/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/after_start.py new file mode 100644 index 00000000..de8e256b --- /dev/null +++ b/src/liteyuki_plugins/scheduled_tasks/divided_by_lifespan/after_start.py @@ -0,0 +1,23 @@ +# -*- coding: utf-8 -*- +""" +Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved + +@Time : 2024/8/15 下午11:32 +@Author : snowykami +@Email : snowykami@outlook.com +@File : after_start.py +@Software: PyCharm +""" +import time + +from liteyuki import get_bot + +liteyuki = get_bot() + + +@liteyuki.on_after_start +def save_startup_timestamp(): + """ + 储存启动的时间戳 + """ + startup_timestamp = time.time()