Crates.io | tokio-blocking |
lib.rs | tokio-blocking |
version | 0.1.1 |
source | src |
created_at | 2019-06-24 01:33:59.108849 |
updated_at | 2019-07-04 00:16:00.144014 |
description | A 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 | |
id | 143084 |
size | 13,852 |
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:
select
doesn't work well).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.
and_then_block
or_else_block
then_block