Use tokio::time::sleep instead of blocking threads

Great explanation by @LarsWirzenius on the why

> Async code in Rust runs in one or more threads. Usually there's a
small number of threads that are multiplexed between different async
functions. The async executor chooses what threads to use and which
thread runs which async code. If the async code takes a long time, such
as by calling std::thread::sleep, the whole thread stops doing anything
else, and then no other async code runs in that thread, either.
This commit is contained in:
Sebastian Martinez
2024-10-25 12:48:33 +02:00
parent 0693f4f806
commit e0323e34bc
3 changed files with 4 additions and 2 deletions
Generated
+1
View File
@@ -4006,6 +4006,7 @@ dependencies = [
"tauri-plugin-shell",
"tauri-plugin-window-state",
"thiserror",
"tokio",
"ts-rs",
]
+1
View File
@@ -31,6 +31,7 @@ tauri-plugin-shell = { version = "2.0.1" }
tauri-plugin-window-state = { version = "2.0.1" }
thiserror = { version = "1.0.64" }
ts-rs = { version = "10.0.0", features = ["serde-json-impl", "no-serde-warnings"] }
tokio = { version = "1.40.0", features = ["time"] }
[features]
# by default Tauri runs in production mode
+2 -2
View File
@@ -45,7 +45,7 @@ pub fn run() {
tauri::async_runtime::spawn(async move {
loop {
let _ = node_handler.emit("node_running", node_status.is_running());
std::thread::sleep(std::time::Duration::from_secs(2));
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
});
@@ -61,7 +61,7 @@ pub fn run() {
log::debug!("node: event subscription loop has exited.");
}
std::thread::sleep(std::time::Duration::from_secs(2));
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
}
});