Struct ProcessSet

Source
pub struct ProcessSet<K> { /* private fields */ }

Implementations§

Source§

impl<K> ProcessSet<K>

Source

pub fn new() -> Self

Examples found in repository?
examples/sleep.rs (line 12)
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}
Source

pub fn with_concurrency_limit(limit: usize) -> Self

Examples found in repository?
examples/concurrency_limit.rs (line 16)
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>

Source

pub fn add_command(&mut self, key: K, command: Command)

Examples found in repository?
examples/sleep.rs (line 13)
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
Hide additional examples
examples/concurrency_limit.rs (line 18)
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

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?
examples/sleep.rs (line 19)
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
Hide additional examples
examples/concurrency_limit.rs (line 29)
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

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.

Source

pub fn sigkill_all(&mut self) -> Result<()>

Kills all subprocesses.

Source

pub fn sigint_all(&mut self) -> Result<()>

Send a SIGINT to all subprocesses and return immediately.

Source

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?
examples/sleep.rs (line 26)
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
Hide additional examples
examples/concurrency_limit.rs (line 36)
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}

Trait Implementations§

Source§

impl<K: Debug> Debug for ProcessSet<K>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K> Default for ProcessSet<K>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<K> Freeze for ProcessSet<K>

§

impl<K> !RefUnwindSafe for ProcessSet<K>

§

impl<K> Send for ProcessSet<K>
where K: Send,

§

impl<K> Sync for ProcessSet<K>
where K: Sync,

§

impl<K> Unpin for ProcessSet<K>
where K: Unpin,

§

impl<K> !UnwindSafe for ProcessSet<K>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.