2 releases
0.1.1 | Jul 4, 2019 |
---|---|
0.1.0 | Jun 24, 2019 |
#43 in #insert
10KB
218 lines
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:
- requires much boilerplate.
- 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.
and_then_block
or_else_block
then_block
Dependencies
~4.5MB
~68K SLoC