Expand description
用 Python 编写 Tauri 后端的 Tauri 2 插件(插件名 py-runtime)。
插件在 setup 阶段启动一个 Python 子进程(sidecar),通过其 stdin / stdout 上的长度前缀帧协议
在前端与 Python 之间转发命令调用、事件和 channel 数据流;Python 的 stderr 按行转发到 log。
Python 侧使用 tauri-runtime SDK(tauri_runtime),前端使用 tauri-plugin-py-runtime-api。
§架构
前端 -> Tauri 命令(commands)-> bridge::Bridge -> bridge::Writer /
bridge::protocol::FrameCodec -> Python 进程;runtime::Supervisor 负责进程生命周期
(启动、握手、崩溃重启、关闭)。
§快速开始
use std::time::Duration;
let _app = tauri::Builder::default().plugin(
tauri_plugin_py_runtime::Builder::new()
.script("../src-python/main.py")
.invoke_timeout(Duration::from_secs(60))
.build(),
);也可以只通过 tauri.conf.json 配置(Builder 上设置的值优先),此时使用 init:
{
"plugins": {
"py-runtime": {
"python": "python3",
"script": "../src-python/main.py",
"invokeTimeoutMs": 30000,
"restartOnCrash": false
}
}
}前端需要在 capability 中加入 "py-runtime:default"。Rust 代码可通过 PyRuntimeExt
(app.py_runtime().supervisor())直接调用 Python 命令或查询状态。
帧协议、错误码与进程生命周期的完整说明见仓库 README。
Modules§
Structs§
- Builder
- 插件构建器。未设置的选项回退到
tauri.conf.json中的Config,再回退到默认值。 - Config
tauri.conf.json中的plugins.py-runtime配置,Builder上设置的值优先。- Error
- 结构化错误,序列化为
{ code, message, data? }返回给前端。 - PyRuntime
- 由插件注册到 Tauri 状态中的运行时句柄,通过
PyRuntimeExt::py_runtime获取。
Traits§
- PyRuntime
Ext - 为
AppHandle、App、Window等实现了Manager的类型提供PyRuntime访问入口。
Functions§
- init
- 仅使用
tauri.conf.json中的plugins.py-runtime初始化插件,等价于Builder::new().build()。