tokio-blocking

Crates.iotokio-blocking
lib.rstokio-blocking
version0.1.1
sourcesrc
created_at2019-06-24 01:33:59.108849
updated_at2019-07-04 00:16:00.144014
descriptionA thin wrapper to provide a simple interface to insert blocking operations between non-blocking operations in the context of futures
homepage
repository
max_upload_size
id143084
size13,852
Yushi OMOTE (YushiOMOTE)

documentation

README

tokio-blocking

A thin wrapper to provide a simple interface to insert blocking operations between non-blocking operations in the context of futures.

    let mut runtime = Runtime::new().unwrap();
    let pool = ThreadPool::new(4);

    let task = lazy(|| Ok::<_, ()>(3))
        .and_then(|_| {
            // Normal non-blocking operations
            Ok(())
        })
        .and_then_block(pool, move |_| {
            // Allow blocking operation, which doesn't actually block other futures
            std::thread::sleep(std::time::Duration::from_secs(3));
            Ok(10)
        })
        .and_then(|_| {
            // Normal non-blocking operation
            Ok(())
        });

    runtime.block_on(task).unwrap();

This crate was born because the existing blocking:

  1. requires much boilerplate.
  2. blocks the entire task after all (e.g. select doesn't work well).

Combinators

The following combinators are supported. All of them takes two arguments: one is the handle of the thread pool which takes care of the blocking operation , and the other is the callback to be called after the blocking operation gets completed. The type of the return value of the callback is Result. It's passed to to the subsequent combinators as the normal combinators (such as and_then) do.

  1. and_then_block
  2. or_else_block
  3. then_block
Commit count: 0

cargo fmt