#How do I feed data asynchronously to a loop running in it's own thread?

4 messages · Page 1 of 1 (latest)

mossy stone
#

I'm making a Tauri project and want to have two background processes on top of the Tauri process

Process 1 runs AI inferences infinitely by taking a picture of the screen every frame
Process 2 gets data fed from process 1 (via mpsc::channel) and performs heavy calculations

Both process 1 and 2 are infinite loops and need to run alongside the Tauri UI process

My optimal setup would be:

  • Tauri process writes to process 1 and 2, and receives data from process 1 and 2
  • Process 1 writes to process 2

Currently all I have it:

  • Process 1 writes to process 2
  • Tauri does not even run properly 😭 😭

Is what I want to achieve possible?

#

here's some pseudocode to get a basic feel of my app:

fn main() {
  let (sender, receiver) = mpsc::channel();
  let receifer = Arc::new(Mutex::new(receifer));

  // Spawn process 1 (AI inference loop)
  thread::spawn(move || infer_process(Arc::new(Mutex::new(sender))));

  // Spawn process 2 (calculation loop)
  let mut app = App {
    channel: Arc::clone(&receiver)
  };

  app.run(); // Process stops here, as this is an infinite loop

  //.. Rest of the code, including the tauri builder, which never runs
}
#

super new to threading and stuff so any help is apprecaited 🙏

raw dune
# mossy stone here's some pseudocode to get a basic feel of my app: ```rs fn main() { let (s...

Pseudocode for what you should be doing

fn main () {
  App::builder()
    .setup(|app| {
      let (sender, receiver) = tokio::sync::mpsc::channel();
      // Thread 1
      tauri::async_runtime::spawn(async {});
      // Thread 2
      tauri::async_runtime::spawn(async {});
      Ok(())
    })
    .run()
}

Both your background threads have to be separated from the main thread. Tauri takes care of the main thread. You have to use the Tokio mpsc for communication because otherwise it doesn't happen in an async friendly manner. Make sure your separated threads are sometimes running .await in their loops, or else they won't play nicely with the concurrent (not parallel) nature of Tokio