From da7a47ed2fe4f0039c5659b63f05a3c104255367 Mon Sep 17 00:00:00 2001 From: chakcy <947105045@qq.com> Date: Fri, 14 Aug 2026 18:07:06 +0800 Subject: [PATCH] =?UTF-8?q?modify:=20=E6=96=87=E6=A1=A3=E8=A1=A5=E5=85=85?= =?UTF-8?q?=20Plugin=20=E5=9F=BA=E7=B1=BB=E8=87=AA=E5=8A=A8=E5=BB=BA?= =?UTF-8?q?=E5=BA=93=E7=94=A8=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 1370d41..ace8f56 100644 --- a/README.md +++ b/README.md @@ -80,18 +80,34 @@ finally: ### 插件中的典型用法 +推荐通过 `Plugin` 基类自动建库(`db_name` 指定文件名,`data_dir` 取自运行时配置), +`on_init` 中即可拿到 `self.db`: + ```python # src/plugins//__init__.py +from chongming_worker.plugin import Plugin + +class MyPlugin(Plugin): + type = "" + db_name = ".db" # 基类自动在 data_dir 下建库并建表 + + async def on_init(self): + db = self.db # 已由基类 init_database() 初始化 + self.set_var("db", db) # 发布到 global_var 供业务代码读取 + ... + +# cleanup 时基类自动 dispose(无需手写) +``` + +不依赖基类时也可手动使用: + +```python from pathlib import Path from chongming_database import Database -from .global_var import set_db -async def create_workers(session, config, count, worker_manager): - db_path = Path(config.get("data_dir", "./data")) / ".db" - db = Database(db_path) - await db.initialize() - set_db(db) - ... +db_path = Path(config.get("data_dir", "./data")) / ".db" +db = Database(db_path) +await db.initialize() ``` ---