async-runtime: add a way to spawn task using the runtime

Summary: This API will be used to spawn tasks in the runtime.

Reviewed By: andll

Differential Revision: D26615607

fbshipit-source-id: 6b9892fad755bbe8feb775e9dad457697b2ea1b7
This commit is contained in:
Jun Wu 2021-03-09 11:43:39 -08:00 committed by Facebook GitHub Bot
parent a8d5625490
commit 3db2121dec

View File

@ -26,6 +26,7 @@ use futures::future::Future;
use futures::stream::{BoxStream, Stream, StreamExt};
use once_cell::sync::Lazy;
use tokio::runtime::{Builder as RuntimeBuilder, Runtime};
use tokio::task::JoinHandle;
static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
let nproc = num_cpus::get();
@ -37,6 +38,15 @@ static RUNTIME: Lazy<Runtime> = Lazy::new(|| {
});
pub static STREAM_BUFFER_SIZE: usize = 128;
/// Spawn a task using the runtime.
pub fn spawn<T>(task: T) -> JoinHandle<T::Output>
where
T: Future + Send + 'static,
T::Output: Send + 'static,
{
RUNTIME.spawn(task)
}
/// Blocks the current thread while waiting for the computation defined by the Future `f` to
/// complete.
///