Radish alpha
r
rad:z4D5UCArafTzTQpDZNQRuqswh3ury
Radicle desktop app
Radicle
Git
Use `tokio::time::sleep` instead of blocking threads
Merged did:key:z6MkkfM3...sVz5 opened 1 year ago

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.

check

πŸ‘‰ Workflow runs πŸ‘‰ Branch on GitHub

3 files changed +4 -2 0693f4f8 β†’ e0323e34
modified Cargo.lock
@@ -4006,6 +4006,7 @@ dependencies = [
 "tauri-plugin-shell",
 "tauri-plugin-window-state",
 "thiserror",
+
 "tokio",
 "ts-rs",
]

modified crates/radicle-tauri/Cargo.toml
@@ -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
modified crates/radicle-tauri/src/lib.rs
@@ -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;
                }
            });