pub struct ProcessSet<K> { /* private fields */ }
Implementations§
Source§impl<K> ProcessSet<K>
impl<K> ProcessSet<K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
11fn main() {
12 let mut procs = ProcessSet::new();
13 procs.add_command(3, sleep_cmd(3));
14 procs.add_command(1, sleep_cmd(1));
15 procs.add_command(2, sleep_cmd(2));
16
17 let mut sh = SignalHandler::default();
18 loop {
19 match procs.wait_any(&mut sh) {
20 WaitAnyResult::NoProcessesRunning => {
21 println!("All done");
22 return;
23 }
24 WaitAnyResult::ReceivedTerminationSignal(_) => {
25 println!("Terminating");
26 procs.sigint_all_and_wait(&mut sh).unwrap();
27 return;
28 }
29 WaitAnyResult::Subprocess(id, r) => {
30 println!("Process {} finished: {:?}", id, r);
31 }
32 }
33 }
34}
Sourcepub fn with_concurrency_limit(limit: usize) -> Self
pub fn with_concurrency_limit(limit: usize) -> Self
Examples found in repository?
15fn main() {
16 let mut procs = ProcessSet::with_concurrency_limit(3);
17 for i in 0..5 {
18 procs.add_command((1, i), make_cmd(1));
19 }
20 for i in 0..5 {
21 procs.add_command((2, i), make_cmd(2));
22 }
23 for i in 0..5 {
24 procs.add_command((3, i), make_cmd(3));
25 }
26
27 let mut sh = SignalHandler::default();
28 loop {
29 match procs.wait_any(&mut sh) {
30 WaitAnyResult::NoProcessesRunning => {
31 println!("All done");
32 return;
33 }
34 WaitAnyResult::ReceivedTerminationSignal(_) => {
35 println!("Terminating");
36 procs.sigint_all_and_wait(&mut sh).unwrap();
37 return;
38 }
39 WaitAnyResult::Subprocess(id, r) => {
40 println!("Process \"sleep {} # {}\" finished: {:?}", id.1, id.0, r);
41 }
42 }
43 }
44}
Source§impl<K: Hash + Eq + Clone> ProcessSet<K>
impl<K: Hash + Eq + Clone> ProcessSet<K>
Sourcepub fn add_command(&mut self, key: K, command: Command)
pub fn add_command(&mut self, key: K, command: Command)
Examples found in repository?
11fn main() {
12 let mut procs = ProcessSet::new();
13 procs.add_command(3, sleep_cmd(3));
14 procs.add_command(1, sleep_cmd(1));
15 procs.add_command(2, sleep_cmd(2));
16
17 let mut sh = SignalHandler::default();
18 loop {
19 match procs.wait_any(&mut sh) {
20 WaitAnyResult::NoProcessesRunning => {
21 println!("All done");
22 return;
23 }
24 WaitAnyResult::ReceivedTerminationSignal(_) => {
25 println!("Terminating");
26 procs.sigint_all_and_wait(&mut sh).unwrap();
27 return;
28 }
29 WaitAnyResult::Subprocess(id, r) => {
30 println!("Process {} finished: {:?}", id, r);
31 }
32 }
33 }
34}
More examples
15fn main() {
16 let mut procs = ProcessSet::with_concurrency_limit(3);
17 for i in 0..5 {
18 procs.add_command((1, i), make_cmd(1));
19 }
20 for i in 0..5 {
21 procs.add_command((2, i), make_cmd(2));
22 }
23 for i in 0..5 {
24 procs.add_command((3, i), make_cmd(3));
25 }
26
27 let mut sh = SignalHandler::default();
28 loop {
29 match procs.wait_any(&mut sh) {
30 WaitAnyResult::NoProcessesRunning => {
31 println!("All done");
32 return;
33 }
34 WaitAnyResult::ReceivedTerminationSignal(_) => {
35 println!("Terminating");
36 procs.sigint_all_and_wait(&mut sh).unwrap();
37 return;
38 }
39 WaitAnyResult::Subprocess(id, r) => {
40 println!("Process \"sleep {} # {}\" finished: {:?}", id.1, id.0, r);
41 }
42 }
43 }
44}
Sourcepub fn wait_any(
&mut self,
signal_handler: &mut SignalHandler,
) -> WaitAnyResult<K>
pub fn wait_any( &mut self, signal_handler: &mut SignalHandler, ) -> WaitAnyResult<K>
Wait for any process to finish, and return the corrosponding key and resulting child (or error).
Takes in a signal handler from outside which can be created with
SignalHandler::default()
. This ensures that
signals between waits are not missed.
If multiple processes have finished, this will only return one of them. Call this function in a loop to wait for all processes to finish.
If no process has finished, this will pause the current thread.
If there are no processes running, this will return NoProcessesRunning.
If, during the middle of waiting, the current process gets a SIGINT or SIGTERM,
this will return ReceivedTerminationSignal(signal_number). More signals can be added
via SignalHandler::add_termination_signal
.
Examples found in repository?
11fn main() {
12 let mut procs = ProcessSet::new();
13 procs.add_command(3, sleep_cmd(3));
14 procs.add_command(1, sleep_cmd(1));
15 procs.add_command(2, sleep_cmd(2));
16
17 let mut sh = SignalHandler::default();
18 loop {
19 match procs.wait_any(&mut sh) {
20 WaitAnyResult::NoProcessesRunning => {
21 println!("All done");
22 return;
23 }
24 WaitAnyResult::ReceivedTerminationSignal(_) => {
25 println!("Terminating");
26 procs.sigint_all_and_wait(&mut sh).unwrap();
27 return;
28 }
29 WaitAnyResult::Subprocess(id, r) => {
30 println!("Process {} finished: {:?}", id, r);
31 }
32 }
33 }
34}
More examples
15fn main() {
16 let mut procs = ProcessSet::with_concurrency_limit(3);
17 for i in 0..5 {
18 procs.add_command((1, i), make_cmd(1));
19 }
20 for i in 0..5 {
21 procs.add_command((2, i), make_cmd(2));
22 }
23 for i in 0..5 {
24 procs.add_command((3, i), make_cmd(3));
25 }
26
27 let mut sh = SignalHandler::default();
28 loop {
29 match procs.wait_any(&mut sh) {
30 WaitAnyResult::NoProcessesRunning => {
31 println!("All done");
32 return;
33 }
34 WaitAnyResult::ReceivedTerminationSignal(_) => {
35 println!("Terminating");
36 procs.sigint_all_and_wait(&mut sh).unwrap();
37 return;
38 }
39 WaitAnyResult::Subprocess(id, r) => {
40 println!("Process \"sleep {} # {}\" finished: {:?}", id.1, id.0, r);
41 }
42 }
43 }
44}
Sourcepub fn try_wait_any(&mut self) -> Option<WaitAnyResult<K>>
pub fn try_wait_any(&mut self) -> Option<WaitAnyResult<K>>
Non-blocking version of wait_any. If no process has finished, this will just return None.
Will never return WaitAnyResult::ReceivedTerminationSignal.
Sourcepub fn sigkill_all(&mut self) -> Result<()>
pub fn sigkill_all(&mut self) -> Result<()>
Kills all subprocesses.
Sourcepub fn sigint_all(&mut self) -> Result<()>
pub fn sigint_all(&mut self) -> Result<()>
Send a SIGINT to all subprocesses and return immediately.
Sourcepub fn sigint_all_and_wait(
&mut self,
signal_handler: &mut SignalHandler,
) -> Result<()>
pub fn sigint_all_and_wait( &mut self, signal_handler: &mut SignalHandler, ) -> Result<()>
Send a SIGINT to all subprocesses and wait for them to finish.
Examples found in repository?
11fn main() {
12 let mut procs = ProcessSet::new();
13 procs.add_command(3, sleep_cmd(3));
14 procs.add_command(1, sleep_cmd(1));
15 procs.add_command(2, sleep_cmd(2));
16
17 let mut sh = SignalHandler::default();
18 loop {
19 match procs.wait_any(&mut sh) {
20 WaitAnyResult::NoProcessesRunning => {
21 println!("All done");
22 return;
23 }
24 WaitAnyResult::ReceivedTerminationSignal(_) => {
25 println!("Terminating");
26 procs.sigint_all_and_wait(&mut sh).unwrap();
27 return;
28 }
29 WaitAnyResult::Subprocess(id, r) => {
30 println!("Process {} finished: {:?}", id, r);
31 }
32 }
33 }
34}
More examples
15fn main() {
16 let mut procs = ProcessSet::with_concurrency_limit(3);
17 for i in 0..5 {
18 procs.add_command((1, i), make_cmd(1));
19 }
20 for i in 0..5 {
21 procs.add_command((2, i), make_cmd(2));
22 }
23 for i in 0..5 {
24 procs.add_command((3, i), make_cmd(3));
25 }
26
27 let mut sh = SignalHandler::default();
28 loop {
29 match procs.wait_any(&mut sh) {
30 WaitAnyResult::NoProcessesRunning => {
31 println!("All done");
32 return;
33 }
34 WaitAnyResult::ReceivedTerminationSignal(_) => {
35 println!("Terminating");
36 procs.sigint_all_and_wait(&mut sh).unwrap();
37 return;
38 }
39 WaitAnyResult::Subprocess(id, r) => {
40 println!("Process \"sleep {} # {}\" finished: {:?}", id.1, id.0, r);
41 }
42 }
43 }
44}