Running actix-web from tauri async runtime #2942
-
Hi guys! Have been using tauri for just a day, and have already made a lot of progress. Absolutely amazing tool, thanks so much to everyone who contributed! However, I now feel kind of stuck. I'm trying to run a Rust server powered by actix from a tauri application. The problem is there are two async runtimes, and I think they might be incompatible. Every time I try to run async code, it simply does not execute. My actix server is exported from a Library through a actix_web::App::new()
.bind_rustls(&endpoint, https_config)
.expect(&*format!("Cannot bind to endpoint {}", &endpoint))
.run()
.await?;
Ok(()) I first tried calling my async tauri::async_runtime::spawn(async {
atomic_server_lib::serve::serve(&config_clone)
.await
.expect("could not start server");
}); But So I tried tauri::async_runtime::block_on(async move {
atomic_server_lib::serve::serve(&config_clone).await.unwrap();
}); But of course, this blocks... So tauri doesn't even start! I also tried using the actix runtime in tauri main.rs: #[actix_rt::main]
async fn main() {
actix_rt::spawn(async move {
println!("This will not print");
}); But now whatever is inside spawn will never even run. let system = actix_rt::System::new("atomic-server");
actix_rt::spawn(async move {
println!("I'm printing just fine")
});
system.run().unwrap(); Which does run, but after calling tauri::async_runtime::spawn(async move {
let _r = system.run();
}); ... but I can't call that function in a I'm running the latest beta's of Actix versions:
Here's my project (Atomic-server) branch's Any help / thoughts would be appreciated! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Hmm, i think the easiest (might not be the best way lol) would be to create an actix runtime on a seperate std thread. so in your specific case you'd end up with something like this: fn main() {
let ctx = tauri::generate_context!();
let config: atomic_server_lib::config::Config = atomic_server_lib::config::init()
.map_err(|e| format!("Initialization failed: {}", e))
.expect("failed init config");
let config_clone = config.clone();
std::thread::spawn(move || {
let rt = actix_rt::Runtime::new().unwrap();
rt.block_on(atomic_server_lib::serve::serve(&config_clone));
});
tauri::Builder::default()
.menu(crate::menu::build(&ctx))
.on_menu_event(crate::menu::handle)
.system_tray(crate::system_tray::build())
.on_system_tray_event(move |e, h| {
let cfg = config.clone();
crate::system_tray::handle(e, h, &cfg)
})
.run(ctx)
.expect("Tauri Error.");
} Edit: Should work the same with Alternatively you could do something like this. I think we need the actix beta versions for this to work, didn't even test the stable versions tho. #[tokio::main]
async fn main() {
tokio::task::spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
rt.block_on(async {
let local = tokio::task::LocalSet::new();
local
.run_until(atomic_server_lib::serve::serve(&config_clone))
.await;
});
});
} Not only does this look kinda ugly, it probably can cause problems with tauri running inside an async main function. fn main() {
tauri::async_runtime::spawn(async move {
let rt = tokio::runtime::Handle::current();
let rt_ = rt.clone();
rt.spawn_blocking(move || {
rt_.block_on(async {
let local = tokio::task::LocalSet::new();
local
.run_until(atomic_server_lib::serve::serve(&config_clone))
.await;
})
});
});
} |
Beta Was this translation helpful? Give feedback.
-
Thanks for helping out again, @FabianLars! Running it like this: tauri::async_runtime::spawn(async move {
let mut rt = actix_rt::System::new("tauri-server");
let serv = atomic_server_lib::serve::serve(config_clone);
rt.block_on(serv);
}); Results in this:
If I try the second suggestion: tokio::task::spawn_blocking(move || {
let rt = tokio::runtime::Handle::current();
rt.block_on(async {
let local = tokio::task::LocalSet::new();
local
.run_until(atomic_server_lib::serve::serve(config_clone))
.await
.unwrap()
})
}); I get:
Which seems to be caused by version mismatches of tokio. So I ran Thanks again for your time! |
Beta Was this translation helpful? Give feedback.
Hmm, i think the easiest (might not be the best way lol) would be to create an actix runtime on a seperate std thread. so in your specific case you'd end up with something like this: