| Crates.io | priority-semaphore |
| lib.rs | priority-semaphore |
| version | 0.1.3 |
| created_at | 2025-06-24 08:58:09.947587+00 |
| updated_at | 2025-07-04 15:05:21.178023+00 |
| description | Runtime-agnostic priority-aware async semaphore for Rust. |
| homepage | |
| repository | https://github.com/h-sumiya/priority-semaphore |
| max_upload_size | |
| id | 1724042 |
| size | 51,870 |
Runtime-agnostic priority aware asynchronous semaphore for Rust.
This crate allows tasks to acquire permits with a signed priority. Higher priorities wake first, making it easy to favour important work while still preventing starvation.
acquireageing featureunsafe codeAdd priority-semaphore to your Cargo.toml:
[dependencies]
priority-semaphore = "0.1.2"
use std::sync::Arc;
use priority_semaphore::PrioritySemaphore;
#[tokio::main]
async fn main() {
let sem = Arc::new(PrioritySemaphore::new(1));
let hi = sem.clone();
let h = tokio::spawn(async move {
let _permit = hi.acquire(10).await.unwrap();
println!("high priority job");
});
let lo = sem.clone();
let l = tokio::spawn(async move {
let _permit = lo.acquire(1).await.unwrap();
println!("low priority job");
});
h.await.unwrap();
l.await.unwrap();
}
More examples can be found in the examples directory.
The full API is available at docs.rs.
| Feature | Default | Description |
|---|---|---|
tokio |
✔ | Enable support for the Tokio runtime |
async-std |
❌ | Enable support for async-std |
ageing |
❌ | Simple ageing strategy to reduce starvation |
std |
✔ | Use the standard library |
docsrs |
❌ | Internal feature used by docs.rs |
This project is licensed under either the MIT license or the Apache License 2.0, at your option.