Shell plugin example from docs blocks main thread #10556
-
Im trying to get "Default Gateway" of local network, basically ip of a router with this function, while using tauri shell plugin: pub async fn get_default_gateway(
app_handle: AppHandle,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
let shell = app_handle.shell();
let output = tauri::async_runtime::block_on(async {
if cfg!(target_os = "windows") {
// Execute the .bat file using the shell plugin
shell
.command("cmd")
.args(["/C", "src-tauri/get_default_gateway.bat"]) // Ensure the .bat file is in src-tauri
.output()
.await
.unwrap()
} else {
// Execute the Unix command using the shell plugin
shell
.command("sh")
.args(["-c", "ip route | grep default | awk '{print $3}'"])
.output()
.await
.unwrap()
}
});
// Check if the command was successful
if output.status.success() {
let stdout = String::from_utf8(output.stdout)?.trim().to_string();
if stdout.is_empty() {
return Err(Box::new(MeasureLatencyError::DefaultGatewayError));
}
Ok(stdout)
} else {
// Handle the case where the command fails
let stderr = String::from_utf8(output.stderr)?;
eprintln!("Command failed with stderr: {}", stderr);
Err(Box::new(MeasureLatencyError::DefaultGatewayError))
}
} I used
As I understand This function is not tauri command, I execute it in |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Is the setup hook / tauri builder running inside your own tokio runtime (for example if you have an async main fn annotated with Anyway, i don't see why you're using block_on in an async function, you can just use |
Beta Was this translation helpful? Give feedback.
Is the setup hook / tauri builder running inside your own tokio runtime (for example if you have an async main fn annotated with
#[tokio::main]
) ?Anyway, i don't see why you're using block_on in an async function, you can just use
.await
on the futures you want to await/block on