pub struct Task<T, M = ()> { /* private fields */ }
Expand description
A spawned task.
A Task
can be awaited to retrieve the output of its future.
Dropping a Task
cancels it, which means its future won’t be polled again. To drop the
Task
handle without canceling it, use detach()
instead. To cancel a
task gracefully and wait until it is fully destroyed, use the cancel()
method.
Note that canceling a task actually wakes it and reschedules one last time. Then, the executor
can destroy the task by simply dropping its Runnable
or by invoking
run()
.
§Examples
use smol::{future, Executor};
use std::thread;
let ex = Executor::new();
// Spawn a future onto the executor.
let task = ex.spawn(async {
println!("Hello from a task!");
1 + 2
});
// Run an executor thread.
thread::spawn(move || future::block_on(ex.run(future::pending::<()>())));
// Wait for the task's output.
assert_eq!(future::block_on(task), 3);
Implementations§
Source§impl<T, M> Task<T, M>
impl<T, M> Task<T, M>
Sourcepub fn detach(self)
pub fn detach(self)
Detaches the task to let it keep running in the background.
§Examples
use smol::{Executor, Timer};
use std::time::Duration;
let ex = Executor::new();
// Spawn a deamon future.
ex.spawn(async {
loop {
println!("I'm a daemon task looping forever.");
Timer::after(Duration::from_secs(1)).await;
}
})
.detach();
Sourcepub async fn cancel(self) -> Option<T>
pub async fn cancel(self) -> Option<T>
Cancels the task and waits for it to stop running.
Returns the task’s output if it was completed just before it got canceled, or None
if
it didn’t complete.
While it’s possible to simply drop the Task
to cancel it, this is a cleaner way of
canceling because it also waits for the task to stop running.
§Examples
use smol::{future, Executor, Timer};
use std::thread;
use std::time::Duration;
let ex = Executor::new();
// Spawn a deamon future.
let task = ex.spawn(async {
loop {
println!("Even though I'm in an infinite loop, you can still cancel me!");
Timer::after(Duration::from_secs(1)).await;
}
});
// Run an executor thread.
thread::spawn(move || future::block_on(ex.run(future::pending::<()>())));
future::block_on(async {
Timer::after(Duration::from_secs(3)).await;
task.cancel().await;
});
Sourcepub fn fallible(self) -> FallibleTask<T, M>
pub fn fallible(self) -> FallibleTask<T, M>
Converts this task into a FallibleTask
.
Like Task
, a fallible task will poll the task’s output until it is
completed or cancelled due to its Runnable
being
dropped without being run. Resolves to the task’s output when completed,
or None
if it didn’t complete.
§Examples
use smol::{future, Executor};
use std::thread;
let ex = Executor::new();
// Spawn a future onto the executor.
let task = ex.spawn(async {
println!("Hello from a task!");
1 + 2
})
.fallible();
// Run an executor thread.
thread::spawn(move || future::block_on(ex.run(future::pending::<()>())));
// Wait for the task's output.
assert_eq!(future::block_on(task), Some(3));
use smol::future;
// Schedule function which drops the runnable without running it.
let schedule = move |runnable| drop(runnable);
// Create a task with the future and the schedule function.
let (runnable, task) = async_task::spawn(async {
println!("Hello from a task!");
1 + 2
}, schedule);
runnable.schedule();
// Wait for the task's output.
assert_eq!(future::block_on(task.fallible()), None);
Sourcepub fn is_finished(&self) -> bool
pub fn is_finished(&self) -> bool
Returns true
if the current task is finished.
Note that in a multithreaded environment, this task can change finish immediately after calling this function.
Trait Implementations§
impl<T, M> RefUnwindSafe for Task<T, M>
std
only.impl<T, M> Send for Task<T, M>
impl<T, M> Sync for Task<T, M>
impl<T, M> Unpin for Task<T, M>
impl<T, M> UnwindSafe for Task<T, M>
std
only.Auto Trait Implementations§
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
std
only.std
only.Source§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
alloc
only.Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
alloc
only.Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more