[][src]Function async_task::spawn

pub fn spawn<F, R, S, T>(
    future: F,
    schedule: S,
    tag: T
) -> (Task<T>, JoinHandle<R, T>) where
    F: Future<Output = R> + Send + 'static,
    R: Send + 'static,
    S: Fn(Task<T>) + Send + Sync + 'static,
    T: Send + Sync + 'static, 

Creates a new task.

This constructor returns a Task reference that runs the future and a JoinHandle that awaits its result.

When run, the task polls future. When woken up, it gets scheduled for running by the schedule function. Argument tag is an arbitrary piece of data stored inside the task.

The schedule function should not attempt to run the task nor to drop it. Instead, it should push the task into some kind of queue so that it can be processed later.

If you need to spawn a future that does not implement Send, consider using the spawn_local function instead.

Examples

use crossbeam::channel;

// The future inside the task.
let future = async {
    println!("Hello, world!");
};

// If the task gets woken up, it will be sent into this channel.
let (s, r) = channel::unbounded();
let schedule = move |task| s.send(task).unwrap();

// Create a task with the future and the schedule function.
let (task, handle) = async_task::spawn(future, schedule, ());