获取全局 Runtime,如果不存在则创建 这允许用户不写 #[tokio::main] 也能跑异步驱动
()
| 7 | /// 获取全局 Runtime,如果不存在则创建 |
| 8 | /// 这允许用户不写 #[tokio::main] 也能跑异步驱动 |
| 9 | pub fn get_runtime() -> &'static Runtime { |
| 10 | RUNTIME.get_or_init(|| { |
| 11 | tokio::runtime::Builder::new_multi_thread() |
| 12 | .enable_all() |
| 13 | .worker_threads(2) // 这里的 IO 任务不繁重,2个线程足矣 |
| 14 | .thread_name("rustcv-bg-worker") |
| 15 | .build() |
| 16 | .expect("Failed to create RustCV background runtime") |
| 17 | }) |
| 18 | } |
| 19 | |
| 20 | /// 辅助函数:在后台运行 Future 并阻塞等待结果 |
| 21 | #[allow(dead_code)] |