#loop #easy #tokio #handle #core

tk-easyloop

A thread local loop and other loop helpers

2 releases

Uses old Rust 2015

0.1.1 Jan 30, 2017
0.1.0 Jan 6, 2017

#4 in #handle

Download history 8/week @ 2022-03-09 18/week @ 2022-03-16 11/week @ 2022-03-23 12/week @ 2022-03-30 13/week @ 2022-04-06 14/week @ 2022-04-13 12/week @ 2022-04-20 27/week @ 2022-04-27 21/week @ 2022-05-04 43/week @ 2022-05-11 18/week @ 2022-05-18 18/week @ 2022-05-25 28/week @ 2022-06-01 10/week @ 2022-06-08 15/week @ 2022-06-15 22/week @ 2022-06-22

77 downloads per month
Used in fewer than 6 crates

MIT/Apache

9KB
56 lines

Tokio Easy Loop

Status:Beta
Documentation:http://docs.rs/tk-easyloop/

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 convenient run 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.

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~5MB
~78K SLoC