pub struct Builder { /* private fields */ }Expand description
插件构建器。未设置的选项回退到 tauri.conf.json 中的 Config,再回退到默认值。
唯一必填项是入口脚本(Builder::script 或 plugins.py-runtime.script)。
§Examples
use std::time::Duration;
let plugin = tauri_plugin_py_runtime::Builder::new()
.script("../src-python/main.py")
.python("python3")
.args(["--verbose"])
.env("APP_MODE", "dev")
.shutdown_timeout(Duration::from_secs(5))
.restart_on_crash(true)
.build::<tauri::Wry>();
let _app = tauri::Builder::default().plugin(plugin);Implementations§
Source§impl Builder
impl Builder
Sourcepub fn python(self, program: impl Into<String>) -> Self
pub fn python(self, program: impl Into<String>) -> Self
设置 Python 解释器(路径或命令名)。
未设置时,若脚本目录下存在 .venv(如 uv sync 创建的)则使用其中的解释器,
否则为 python3(Windows 上为 python)。该解释器必须能导入 tauri_runtime
(及其依赖 pydantic)。
Sourcepub fn script(self, path: impl Into<PathBuf>) -> Self
pub fn script(self, path: impl Into<PathBuf>) -> Self
设置 Python 入口脚本。
绝对路径原样使用。相对路径按以下顺序尝试,第一个存在的文件胜出:仅在 debug 构建中,
相对于当前工作目录(tauri dev 下为 src-tauri);然后在所有构建中,相对于资源目录,
并把每个 .. 替换为 _up_(与打包器布局一致)。因此当 bundle.resources 包含
"../src-python/**/*" 时,"../src-python/main.py" 在开发与发布构建中都能找到。
Python 以脚本所在目录作为工作目录运行。
Sourcepub fn bundle(self, name: impl Into<String>) -> Self
pub fn bundle(self, name: impl Into<String>) -> Self
设置 Nuitka 打包产物名(即 tauri-runtime build 配置中的 name)。
仅在 release 构建中生效:运行资源目录下的 <name>/<name>(Windows 为 <name>.exe),
以其所在目录为工作目录,并忽略 Builder::python 与 Builder::script;debug 构建
仍以解释器运行脚本。产物需通过 bundle.resources 打进应用,找不到时 setup 失败。
Sourcepub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
pub fn env(self, key: impl Into<String>, value: impl Into<String>) -> Self
为 Python 进程追加一个环境变量,可多次调用。
PYTHONUNBUFFERED=1 与 PYTHONIOENCODING=utf-8 总会被设置,无法覆盖。
Sourcepub fn invoke_timeout(self, timeout: Duration) -> Self
pub fn invoke_timeout(self, timeout: Duration) -> Self
设置每次调用的默认超时,默认 30 秒。前端的 timeoutMs 最多可将其放宽到该值的 10 倍。
Sourcepub fn shutdown_timeout(self, timeout: Duration) -> Self
pub fn shutdown_timeout(self, timeout: Duration) -> Self
设置 Python 执行 shutdown 钩子的时间预算,默认 3 秒;超过该时间再加 1 秒宽限后进程会被杀死。
Sourcepub fn handshake_timeout(self, timeout: Duration) -> Self
pub fn handshake_timeout(self, timeout: Duration) -> Self
设置 Python 启动并发送握手的时限,默认 10 秒;也是调用等待启动中的运行时就绪的最长时间。
Sourcepub fn restart_on_crash(self, enabled: bool) -> Self
pub fn restart_on_crash(self, enabled: bool) -> Self
设置 Python 意外退出后是否以指数退避自动重启,默认关闭。
Sourcepub fn build<R: Runtime>(self) -> TauriPlugin<R, Option<Config>>
pub fn build<R: Runtime>(self) -> TauriPlugin<R, Option<Config>>
构建 Tauri 插件。
插件 setup 时解析配置与入口脚本、注册 PyRuntime 状态并在后台启动 Python;
应用退出(RunEvent::Exit)时阻塞等待 Supervisor::shutdown 完成。
未配置入口脚本或脚本不存在时 setup 失败,错误信息列出所有尝试过的路径。