Best way to have long-running update loop #4775
Unanswered
AndrewJSchoen
asked this question in
Q&A
Replies: 1 comment
-
|
I believe it's too late to answer this, but just keeping it here since this sounds like a common use cases people are running into like me, and I didn't find any solution in issues/discussions. So here's something I gathered with trial and error: Pretty sure some optimizations are possible, I am not exactly a rust pro. Firstly, you need your state to be Arc<Muxtex<>> so that you can share it in thread tauri::Builder::default()
.manage(AppState(Arc::new(Mutex::new(Some(db))))) // <---- state is Arc<Mutex<db>>
.invoke_handler(tauri::generate_handler![ start ]) // <--------- register your command
.run(tauri::generate_context!())
.expect("error while running tauri application");Rest is pretty straighforward, just create your tauri command, make it async, accept the state, lock the mutex etc etc #[tauri::command]
async fn get_cpu_info<R: Runtime>(
window: tauri::Window<R>,
state: State<'_, AppState>,
) -> Result<(), String> {
let state = Arc::clone(&state.0);
thread::spawn(move || loop {
let lock = state .try_lock();
// Handle errors, unwrap if you want
if lock.is_err() {
break;
}
let mut lock = match lock {
Ok(x) => x,
Err(_) => break,
};
// Here's your state
let db = lock.as_mut().unwrap();
// use it however you want, you can emit an event to FE as well.
});
Ok(())
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey all,
Sorry if this is a naive question, so forgive me if my rust knowledge is a bit lacking. I have a system that will have a long-running loop running, and I want to eventually emit changes within that loop to the frontend. However, I am just working on getting the loop to run without errors, and I thought it would be useful to double-check that generally speaking, I am taking the best approach. To start off, there are a couple considerations that I have that may or may not be specific to my code.
So I will get into a bit of implementation. I have generalized the structs and commented them a bit to give some explanation.
Then, I wrap the BrainHandler in a mutex and specify how it is instantiated
I tried a couple things, but couldn't get the loop to execute within the tauri command.
Entirely possible that I am not doing it right, and that this is the way to do it.
So, I tried just wrapping an inner function of the BrainHandler. Note, this still raises errors saying the BrainHandler doesn't implement "Copy"
Finally, I specify the main function in Tauri
So that is generally what I am trying to do. I know I could keep trying to fix the various issues one by one, but I just wanted to do a bit of a sanity check with the pros here to see if this is even the right way to go about things. If the general logic checks out, any suggestions on how to handle instantiating and executing the loop? Any feedback would be greatly appreciated. Thanks!
Beta Was this translation helpful? Give feedback.
All reactions