surrealdb/exe/
spawn.rs

1#![cfg(not(target_arch = "wasm32"))]
2
3use executor::{Executor, Task};
4use once_cell::sync::Lazy;
5use std::future::Future;
6use std::panic::catch_unwind;
7
8pub fn spawn<T: Send + 'static>(future: impl Future<Output = T> + Send + 'static) -> Task<T> {
9	static GLOBAL: Lazy<Executor<'_>> = Lazy::new(|| {
10		std::thread::spawn(|| {
11			catch_unwind(|| {
12				futures::executor::block_on(GLOBAL.run(futures::future::pending::<()>()))
13			})
14			.ok();
15		});
16		Executor::new()
17	});
18	GLOBAL.spawn(future)
19}