Expand description
A main loop wrapper around tokio to provide thread-local loop which:
-
Avoids padding a
Handle
in to every function -
Mostly avoids common error:
thread 'foo' panicked at 'no Task is currently running'
, by providing convenientrun
function for all your code involving futures
§Example
extern crate futures;
extern crate tk_easyloop;
use std::time::Duration;
use tk_easyloop::{run, timeout};
fn main() {
run(|| {
// should return some future, let's use a timeout
timeout(Duration::new(1, 0))
}).unwrap();
}
§Multi-threaded Example
This crate uses thread-local storage for storing loop, but it doesn’t mean multi-treading doesn’t work. Multiple threads can be used too.
extern crate tk_easyloop;
use std::thread;
use std::time::Duration;
use tk_easyloop::{run, timeout};
fn main() {
let mut threads = Vec::new();
for thread_no in 0..10 {
threads.push(thread::spawn(move || {
run(|| {
timeout(Duration::new(1, 0))
})
}))
}
for t in threads {
t.join().unwrap().unwrap();
}
}
See examples/multi-threaded.rs
for more comprehensive example.
Functions§
- handle
- Returns current loop handle
- interval
- Create an interval tied to the current loop
- is_
running - Returns
true
if there is an event loop currently running - run
- Run the main loop and initialize it by running a function
- run_
forever - Run the main loop and initialize it by running a function, which spawns more futures in the main loop. Then run loop indefinitely.
- spawn
- Spawn a future to the current main loop
- spawn_
fn - Spawn a closure to the current main loop
- timeout
- Create a timeout tied to the current loop
- timeout_
at - Create a timeout tied to the current loop