{"id":13648519,"url":"https://github.com/smol-rs/blocking","last_synced_at":"2025-05-14T06:13:59.890Z","repository":{"id":43220109,"uuid":"262637792","full_name":"smol-rs/blocking","owner":"smol-rs","description":"A thread pool for isolating blocking I/O in async programs","archived":false,"fork":false,"pushed_at":"2025-01-19T16:20:55.000Z","size":132,"stargazers_count":357,"open_issues_count":3,"forks_count":20,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-08T00:08:53.188Z","etag":null,"topics":["rust"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/smol-rs.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE-APACHE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-09T18:51:18.000Z","updated_at":"2025-05-05T14:00:12.000Z","dependencies_parsed_at":"2024-02-26T01:49:47.911Z","dependency_job_id":"f6afff17-04e9-4855-94ed-1ac558b999d9","html_url":"https://github.com/smol-rs/blocking","commit_stats":{"total_commits":146,"total_committers":11,"mean_commits":"13.272727272727273","dds":0.5273972602739726,"last_synced_commit":"34d94720761fb5d59e52b450ce71b6a843523e83"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smol-rs%2Fblocking","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smol-rs%2Fblocking/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smol-rs%2Fblocking/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/smol-rs%2Fblocking/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/smol-rs","download_url":"https://codeload.github.com/smol-rs/blocking/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254076853,"owners_count":22010612,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["rust"],"created_at":"2024-08-02T01:04:19.287Z","updated_at":"2025-05-14T06:13:59.871Z","avatar_url":"https://github.com/smol-rs.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# blocking\n\n[![Build](https://github.com/smol-rs/blocking/workflows/Build%20and%20test/badge.svg)](\nhttps://github.com/smol-rs/blocking/actions)\n[![License](https://img.shields.io/badge/license-Apache--2.0_OR_MIT-blue.svg)](\nhttps://github.com/smol-rs/blocking)\n[![Cargo](https://img.shields.io/crates/v/blocking.svg)](\nhttps://crates.io/crates/blocking)\n[![Documentation](https://docs.rs/blocking/badge.svg)](\nhttps://docs.rs/blocking)\n\nA thread pool for isolating blocking I/O in async programs.\n\nSometimes there's no way to avoid blocking I/O. Consider files or stdin, which have weak async\nsupport on modern operating systems. While [IOCP], [AIO], and [io_uring] are possible\nsolutions, they're not always available or ideal.\n\nSince blocking is not allowed inside futures, we must move blocking I/O onto a special thread\npool provided by this crate. The pool dynamically spawns and stops threads depending on the\ncurrent number of running I/O jobs.\n\nNote that there is a limit on the number of active threads. Once that limit is hit, a running\njob has to finish before others get a chance to run. When a thread is idle, it waits for the\nnext job or shuts down after a certain timeout.\n\nThe default number of threads (set to 500) can be altered by setting BLOCKING_MAX_THREADS environment variable with value between 1 and 10000.\n\n[IOCP]: https://en.wikipedia.org/wiki/Input/output_completion_port\n[AIO]: http://man7.org/linux/man-pages/man2/io_submit.2.html\n[io_uring]: https://lwn.net/Articles/776703\n\n## Examples\n\nRead the contents of a file:\n\n```rust\nuse blocking::unblock;\nuse std::fs;\n\nlet contents = unblock(|| fs::read_to_string(\"file.txt\")).await?;\nprintln!(\"{}\", contents);\n```\n\nRead a file and pipe its contents to stdout:\n\n```rust\nuse blocking::{unblock, Unblock};\nuse futures_lite::io;\nuse std::fs::File;\n\nlet input = unblock(|| File::open(\"file.txt\")).await?;\nlet input = Unblock::new(input);\nlet mut output = Unblock::new(std::io::stdout());\n\nio::copy(input, \u0026mut output).await?;\n```\n\nIterate over the contents of a directory:\n\n```rust\nuse blocking::Unblock;\nuse futures_lite::prelude::*;\nuse std::fs;\n\nlet mut dir = Unblock::new(fs::read_dir(\".\")?);\nwhile let Some(item) = dir.next().await {\n    println!(\"{}\", item?.file_name().to_string_lossy());\n}\n```\n\nSpawn a process:\n\n```rust\nuse blocking::unblock;\nuse std::process::Command;\n\nlet out = unblock(|| Command::new(\"dir\").output()).await?;\n```\n\n## License\n\nLicensed under either of\n\n * Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)\n * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)\n\nat your option.\n\n#### Contribution\n\nUnless you explicitly state otherwise, any contribution intentionally submitted\nfor inclusion in the work by you, as defined in the Apache-2.0 license, shall be\ndual licensed as above, without any additional terms or conditions.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmol-rs%2Fblocking","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmol-rs%2Fblocking","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmol-rs%2Fblocking/lists"}