#thread #cancel #future #utilities #tokio

wombo

Utilities for managing event loop threads

3 releases

Uses old Rust 2015

0.1.2 Jan 27, 2018
0.1.1 Jan 27, 2018
0.1.0 Jan 19, 2018

#1578 in Asynchronous

Download history 135/week @ 2023-11-30 189/week @ 2023-12-07 40/week @ 2023-12-14 15/week @ 2023-12-21 38/week @ 2024-01-04 167/week @ 2024-01-11 275/week @ 2024-01-18 249/week @ 2024-01-25 229/week @ 2024-02-01 302/week @ 2024-02-08 245/week @ 2024-02-15 166/week @ 2024-02-22 185/week @ 2024-02-29 406/week @ 2024-03-07 548/week @ 2024-03-14

1,326 downloads per month

MIT license

27KB
600 lines

Wombo

Build Status Crates.io

Documentation

Utilities for managing event loop threads.

Install

With cargo edit.

cargo add wombo

Features

  • Less boilerplate spawning event loop threads.
  • Cancel event loops, with an interface to clean up resources on the event loop first.
  • Receive notifications when event loop threads exit, with arbitrary data.

Usage

Spawn an event loop thread that waits for a second, and conditionally interrupt it after 50 ms, returning some data to the main thread.


extern crate futures;
extern crate wombo;
extern crate tokio_timer;

use futures::Future;
use tokio_timer::Timer;

use std::thread;
use std::time::Duration;

use wombo::*;

const SHOULD_INTERRUPT: bool = true;

fn main() {
  // expected value when not interrupted
  let foo = 1;
  // expected value when interrupted
  let bar = 2;
    
  let timer = Timer::default();
  let sleep_dur = Duration::from_millis(50);
  
  let wombo = Wombo::new();
  let options = ThreadOptions::new("loop-1", None);
  
  let spawn_result = wombo.spawn(options, move |handle, hooks| {
    // sleep for a second, then return foo, or if canceled return bar
    let dur = Duration::from_millis(1000);
    
    hooks.on_cancel(move || Ok(bar));
  
    timer.sleep(dur)
      .map_err(|_| ())
      .and_then(move |_| Ok(foo))
  });
  
  if let Err(e) = spawn_result {
    panic!("Error spawning event loop thread: {:?}", e);
  }
  
  // give the child thread a chance to initialize
  thread::sleep(sleep_dur);
  
  assert!(wombo.is_running());
  println!("Wombo {} running thread {:?}", wombo.id(), wombo.core_thread_id());
  
  if SHOULD_INTERRUPT {
    if let Err(e) = wombo.cancel() {
      panic!("Error canceling: {:?}", e);
    }
  }
  
  let result = match wombo.on_exit() {
    Ok(rx) => rx.wait().unwrap(),
    Err(e) => panic!("Error calling on_exit: {:?}", e)
  };
  
  if SHOULD_INTERRUPT {
    assert_eq!(result, Some(bar));
  }else{
    assert_eq!(result, Some(foo));
  }
}

See examples for more.

Tests

To run the tests:

cargo test

Dependencies

~10MB
~178K SLoC