• Stars
    star
    278
  • Rank 143,503 (Top 3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 4 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A thread pool for isolating blocking I/O in async programs

blocking

Build License Cargo Documentation

A thread pool for isolating blocking I/O in async programs.

Sometimes there's no way to avoid blocking I/O. Consider files or stdin, which have weak async support on modern operating systems. While IOCP, AIO, and io_uring are possible solutions, they're not always available or ideal.

Since blocking is not allowed inside futures, we must move blocking I/O onto a special thread pool provided by this crate. The pool dynamically spawns and stops threads depending on the current number of running I/O jobs.

Note that there is a limit on the number of active threads. Once that limit is hit, a running job has to finish before others get a chance to run. When a thread is idle, it waits for the next job or shuts down after a certain timeout.

The default number of threads (set to 500) can be altered by setting BLOCKING_MAX_THREADS environment variable with value between 1 and 10000.

Examples

Read the contents of a file:

use blocking::unblock;
use std::fs;

let contents = unblock(|| fs::read_to_string("file.txt")).await?;
println!("{}", contents);

Read a file and pipe its contents to stdout:

use blocking::{unblock, Unblock};
use futures_lite::io;
use std::fs::File;

let input = unblock(|| File::open("file.txt")).await?;
let input = Unblock::new(input);
let mut output = Unblock::new(std::io::stdout());

io::copy(input, &mut output).await?;

Iterate over the contents of a directory:

use blocking::Unblock;
use futures_lite::prelude::*;
use std::fs;

let mut dir = Unblock::new(fs::read_dir(".")?);
while let Some(item) = dir.next().await {
    println!("{}", item?.file_name().to_string_lossy());
}

Spawn a process:

use blocking::unblock;
use std::process::Command;

let out = unblock(|| Command::new("dir").output()).await?;

License

Licensed under either of

at your option.

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.

More Repositories

1

smol

A small and fast async runtime for Rust
Rust
2,871
star
2

async-channel

Async multi-producer multi-consumer channel
Rust
524
star
3

async-io

Async I/O and timers
Rust
330
star
4

polling

Portable interface to epoll, kqueue, event ports, and wepoll
Rust
329
star
5

futures-lite

Futures, streams, and async I/O combinators.
Rust
293
star
6

fastrand

A simple and fast random number generator
Rust
287
star
7

event-listener

Notify async tasks or threads
Rust
274
star
8

async-task

Task abstraction for building executors
Rust
255
star
9

async-lock

Async synchronization primitives
Rust
193
star
10

concurrent-queue

Concurrent multi-producer multi-consumer queue
Rust
180
star
11

async-executor

Async executor
Rust
179
star
12

async-process

Async interface for working with processes
Rust
124
star
13

async-compat

Compatibility adapter between tokio and futures
Rust
120
star
14

async-net

Async networking primitives for TCP/UDP/Unix communication
Rust
112
star
15

async-fs

Async filesystem primitives
Rust
110
star
16

easy-parallel

Run closures in parallel
Rust
100
star
17

async-broadcast

Async broadcast channels
Rust
96
star
18

parking

Thread parking and unparking
Rust
55
star
19

cache-padded

Prevent false sharing by padding and aligning to the length of a cache line
Rust
50
star
20

waker-fn

Convert closures into wakers
Rust
38
star
21

vec-arena

[DEPRECATED] A simple object arena
Rust
38
star
22

async-rustls

Async TLS/SSL streams using rustls
Rust
32
star
23

async-dup

Duplicate an async I/O handle
Rust
31
star
24

atomic-waker

futures::task::AtomicWaker extracted into its own crate
Rust
18
star
25

nb-connect

[DEPRECATED] Non-blocking TCP or Unix connect
Rust
15
star
26

piper

An asynchronous single-consumer single-producer pipe for bytes
Rust
6
star
27

fastrand-contrib

Extension functionality for the fastrand crate
Rust
3
star
28

async-signal

Asynchronous signal handling
Rust
3
star
29

smol-macros

Macros for using smol-rs
Rust
1
star