[][src]Function smol::run

pub fn run<T>(future: impl Future<Output = T>) -> T

Runs executors and polls the reactor.

This function simultaneously runs the thread-local executor, runs the work-stealing executor, and polls the reactor for I/O events and timers. At least one thread has to be calling run() in order for futures waiting on I/O and timers to get notified.

Examples

Single-threaded executor:

// Run the thread-local and work-stealing executor on the current thread.
smol::run(async {
    println!("Hello from the smol executor!");
});

Multi-threaded executor:

use futures::future;
use smol::Task;
use std::thread;

// Same number of threads as there are CPU cores.
let num_threads = num_cpus::get().max(1);

// Run the thread-local and work-stealing executor on a thread pool.
for _ in 0..num_threads {
    // A pending future is one that simply yields forever.
    thread::spawn(|| smol::run(future::pending::<()>()));
}

// No need to `run()`, now we can just block on the main future.
smol::block_on(async {
    Task::spawn(async {
        println!("Hello from an executor thread!");
    })
    .await;
});

Stoppable multi-threaded executor:

use smol::Task;
use std::thread;

// Same number of threads as there are CPU cores.
let num_threads = num_cpus::get().max(1);

// A channel that sends the shutdown signal.
let (s, r) = piper::chan::<()>(0);
let mut threads = Vec::new();

// Create an executor thread pool.
for _ in 0..num_threads {
    // Spawn an executor thread that waits for the shutdown signal.
    let r = r.clone();
    threads.push(thread::spawn(move || smol::run(r.recv())));
}

// No need to `run()`, now we can just block on the main future.
smol::block_on(async {
    Task::spawn(async {
        println!("Hello from an executor thread!");
    })
    .await;
});

// Send a shutdown signal.
drop(s);

// Wait for threads to finish.
for t in threads {
    t.join().unwrap();
}