https://github.com/micromaomao/rust-spawn-wait
Spawn and manage a set of processes each associated with a key, and wait on all or part of them simultaneously.
https://github.com/micromaomao/rust-spawn-wait
Last synced: about 2 months ago
JSON representation
Spawn and manage a set of processes each associated with a key, and wait on all or part of them simultaneously.
- Host: GitHub
- URL: https://github.com/micromaomao/rust-spawn-wait
- Owner: micromaomao
- License: mit
- Created: 2022-02-08T17:25:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-02-09T00:36:34.000Z (over 3 years ago)
- Last Synced: 2025-03-01T23:27:24.938Z (3 months ago)
- Language: Rust
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://crates.io/crates/spawn-wait)
[](https://docs.rs/spawn-wait)Easily spawn and manage a set of processes.
```rust
use std::process::Command;use spawn_wait::{ProcessSet, SignalHandler, WaitAnyResult};
fn sleep_cmd(secs: i32) -> Command {
let mut cmd = Command::new("sleep");
cmd.arg(secs.to_string());
cmd
}fn main() {
let mut procs = ProcessSet::new();// You can set a limit on concurrency with ProcessSet::with_concurrency_limit(number).
// Associate each process with a key (Hash + Eq + Clone)
procs.add_command(3, sleep_cmd(3));
procs.add_command(1, sleep_cmd(1));
procs.add_command(2, sleep_cmd(2));let mut sh = SignalHandler::default();
loop {
match procs.wait_any(&mut sh) {
WaitAnyResult::NoProcessesRunning => {
println!("All done");
return;
}
WaitAnyResult::ReceivedTerminationSignal(_) => {
// The crate handles SIGINT for you, so you can exit cleanly
// when the user presses ctrl-c.
println!("Terminating");
procs.sigint_all_and_wait(&mut sh).unwrap();
return;
}
WaitAnyResult::Subprocess(id, r) => {
println!("Process {} finished: {:?}", id, r);
}
}
}
}```