mirror of
https://github.com/tokio-rs/tokio.git
synced 2025-10-01 12:20:39 +00:00
task: add tokio::task::join_set::Builder::spawn_blocking
(#6578)
Signed-off-by: hi-rustin <rustin.liu@gmail.com>
This commit is contained in:
parent
0b651c070f
commit
e62c3e9269
@ -623,6 +623,51 @@ impl<'a, T: 'static> Builder<'a, T> {
|
|||||||
Ok(self.joinset.insert(self.builder.spawn_on(future, handle)?))
|
Ok(self.joinset.insert(self.builder.spawn_on(future, handle)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Spawn the blocking code on the blocking threadpool with this builder's
|
||||||
|
/// settings, and store it in the [`JoinSet`].
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// An [`AbortHandle`] that can be used to remotely cancel the task.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// This method panics if called outside of a Tokio runtime.
|
||||||
|
///
|
||||||
|
/// [`JoinSet`]: crate::task::JoinSet
|
||||||
|
/// [`AbortHandle`]: crate::task::AbortHandle
|
||||||
|
#[track_caller]
|
||||||
|
pub fn spawn_blocking<F>(self, f: F) -> std::io::Result<AbortHandle>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> T,
|
||||||
|
F: Send + 'static,
|
||||||
|
T: Send,
|
||||||
|
{
|
||||||
|
Ok(self.joinset.insert(self.builder.spawn_blocking(f)?))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Spawn the blocking code on the blocking threadpool of the provided
|
||||||
|
/// runtime handle with this builder's settings, and store it in the
|
||||||
|
/// [`JoinSet`].
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// An [`AbortHandle`] that can be used to remotely cancel the task.
|
||||||
|
///
|
||||||
|
/// [`JoinSet`]: crate::task::JoinSet
|
||||||
|
/// [`AbortHandle`]: crate::task::AbortHandle
|
||||||
|
#[track_caller]
|
||||||
|
pub fn spawn_blocking_on<F>(self, f: F, handle: &Handle) -> std::io::Result<AbortHandle>
|
||||||
|
where
|
||||||
|
F: FnOnce() -> T,
|
||||||
|
F: Send + 'static,
|
||||||
|
T: Send,
|
||||||
|
{
|
||||||
|
Ok(self
|
||||||
|
.joinset
|
||||||
|
.insert(self.builder.spawn_blocking_on(f, handle)?))
|
||||||
|
}
|
||||||
|
|
||||||
/// Spawn the provided task on the current [`LocalSet`] with this builder's
|
/// Spawn the provided task on the current [`LocalSet`] with this builder's
|
||||||
/// settings, and store it in the [`JoinSet`].
|
/// settings, and store it in the [`JoinSet`].
|
||||||
///
|
///
|
||||||
|
Loading…
x
Reference in New Issue
Block a user