Start tokio runtime on main thread and enter context there, so non-async code can spawn tasks too

This commit is contained in:
Mike Dilger 2023-01-01 11:54:13 +13:00
parent db6907dea5
commit dd0ce06828

View File

@ -46,11 +46,16 @@ fn main() -> Result<(), Error> {
let settings = crate::settings::Settings::blocking_load()?; let settings = crate::settings::Settings::blocking_load()?;
*GLOBALS.settings.blocking_write() = settings; *GLOBALS.settings.blocking_write() = settings;
// Start async code // We create and enter the runtime on the main thread so that
// We do this on a separate thread because egui is most portable by // non-async code can have a runtime context within which to spawn
// being on the main thread. // async tasks.
let async_thread = thread::spawn(|| {
let rt = tokio::runtime::Runtime::new().unwrap(); let rt = tokio::runtime::Runtime::new().unwrap();
let _main_rt = rt.enter(); // <-- this allows it.
// We run our main async code on a separate thread, not just a
// separate task. This leave the main thread for UI work only.
// egui is most portable when it is on the main thread.
let async_thread = thread::spawn(move || {
rt.block_on(tokio_main()); rt.block_on(tokio_main());
}); });