Function async_task::spawn [−][src]
pub fn spawn<F, S>(future: F, schedule: S) -> (Runnable, Task<F::Output>) where
F: Future + Send + 'static,
F::Output: Send + 'static,
S: Fn(Runnable) + Send + Sync + 'static,
Creates a new task.
The returned Runnable
is used to poll the future
, and the Task
is used to await its
output.
Method run()
polls the task’s future once. Then, the Runnable
vanishes and only reappears when its Waker
wakes the task, thus scheduling it to be run
again.
When the task is woken, its Runnable
is passed to the schedule
function.
The schedule
function should not attempt to run the Runnable
nor to drop it. Instead, it
should push it into a task queue so that it can be processed later.
If you need to spawn a future that does not implement Send
or isn’t 'static
, consider
using spawn_local()
or spawn_unchecked()
instead.
Examples
// The future inside the task. let future = async { println!("Hello, world!"); }; // A function that schedules the task when it gets woken up. let (s, r) = flume::unbounded(); let schedule = move |runnable| s.send(runnable).unwrap(); // Create a task with the future and the schedule function. let (runnable, task) = async_task::spawn(future, schedule);