Compare commits
3 Commits
0e3d7afd52
...
a9b2d4b481
Author | SHA1 | Date | |
---|---|---|---|
|
a9b2d4b481 | ||
8f4d61b9e1 | |||
c072ef26d7 |
19
CHANGELOG.md
Normal file
19
CHANGELOG.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Changelog
|
||||||
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||||
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [0.0.2] - 2025-07-01
|
||||||
|
|
||||||
|
### Docs
|
||||||
|
- correct
|
||||||
|
|
||||||
|
## [0.0.1] - 2025-07-01
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- cut initial version
|
||||||
|
- readme etc.
|
||||||
|
- add basic worker pool
|
@ -3,7 +3,7 @@ members = ["crates/*"]
|
|||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
version = "0.1.0"
|
version = "0.0.2"
|
||||||
|
|
||||||
[workspace.dependencies]
|
[workspace.dependencies]
|
||||||
noworkers = { path = "crates/noworkers" }
|
noworkers = { path = "crates/noworkers" }
|
||||||
|
20
README.md
20
README.md
@ -25,33 +25,39 @@ Then in your code:
|
|||||||
|
|
||||||
```rust
|
```rust
|
||||||
use noworkers::Workers;
|
use noworkers::Workers;
|
||||||
use tokio_util::sync::CancellationToken;
|
|
||||||
```
|
```
|
||||||
|
|
||||||
## Quick Example
|
## Quick Example
|
||||||
|
|
||||||
```rust,no_run
|
```rust
|
||||||
use noworkers::Workers;
|
use noworkers::Workers;
|
||||||
use tokio_util::sync::CancellationToken;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> anyhow::Result<()> {
|
async fn main() -> anyhow::Result<()> {
|
||||||
// Create a worker group with up to 5 concurrent tasks
|
// Create a worker group with up to 5 concurrent tasks
|
||||||
let mut workers = Workers::new();
|
let mut workers = Workers::new();
|
||||||
|
|
||||||
workers
|
// Limit amount of concurrent workers
|
||||||
.with_limit(5)
|
workers.with_limit(5);
|
||||||
.with_cancel(&CancellationToken::new());
|
|
||||||
|
// Adds cancellation signal
|
||||||
|
workers.with_cancel_task(async move {
|
||||||
|
// send cancellation to tasks after 60 seconds
|
||||||
|
tokio::time::sleep(std::time::Duration::from_secs(60)).await
|
||||||
|
});
|
||||||
|
|
||||||
// Spawn 10 async jobs
|
// Spawn 10 async jobs
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
|
// Work is done immediatley, so this will wait in two batches of 1 seconds each (because of limit)
|
||||||
workers.add(move |cancel_token| async move {
|
workers.add(move |cancel_token| async move {
|
||||||
// Respect cancellation, or not, if you don't care about blocking forever
|
// optional tokio::select, if you use cancellation for your tasks, if not just do your work
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
|
// Do work, in this case just sleep
|
||||||
_ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {
|
_ = tokio::time::sleep(tokio::time::Duration::from_secs(1)) => {
|
||||||
println!("Job {i} done");
|
println!("Job {i} done");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
// If we receive cancel we close
|
||||||
_ = cancel_token.cancelled() => {
|
_ = cancel_token.cancelled() => {
|
||||||
println!("Job {i} cancelled");
|
println!("Job {i} cancelled");
|
||||||
Ok(())
|
Ok(())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user