Run a bunch of stuff at once. Keep queueing jobs until you have all of the work done, then wait for everything to finish. Can determine number of failures and get the status codes.
The library adds functions to a Bash environment so your shell scripts can leverage this extra functionality. You include it with BPM.
Add to your bpm.ini
file the following dependency.
[dependencies]
parallel=*
Run bpm install
to add the library. Finally, use it in your scripts.
#!/usr/bin/env bash
. bpm
bpm::include parallel
The number of jobs to run at once. Leave blank to auto detect.
Don't modify this list. It's tracking the PIDs of every job running in the background.
An array containing the list of status codes returned by the backgrounded jobs. This can be reset with parallel::resetStatusCodes
and is automatically wiped out with parallel::finish
.
Preserves the old SIGINT trap. This library layers on top of the SIGINT trap. If you need to also layer your own functionality on top of this library, don't just use trap your_handler SIGINT
but instead make sure to also call the previous SIGINT trap as well.
Do not modify this variable yourself.
The method to use when wating for jobs to finish. wait -n
is faster but only introduced in Bash 4.
Checks that all of the backgrounded jobs are still running. If they are not, this will remove them from the list.
Examples
parallel::curateJobList
Returns nothing.
Wait for all jobs to complete. Resets the status code array. Returns the number of failures. Removes the SIGINT trap that was added in parallel::run
.
- $1 - Destination variable for the status code array, optional.
Returns the number of failures.
SIGINT trap handler for parallelized operations. Stop running jobs and send SIGINT to all children.
Examples
* PARALLEL_OLD_INT_TRAP=$(trap - SIGINT) PARALLEL_OLD_INT_TRAP=${PARALLEL_OLD_INT_TRAP:--} trap parallel::intTrap SIGINT
Returns nothing. Will resend SIGINT to current process after restoring the previous SIGINT trap.
Determines the number of parallel jobs that should be executed. This is executed automatically by the library. It returns the number of CPU processors, defaulting to at least 1.
Example:
PARALLEL_JOBS=$(parallel::getLimt)
Returns nothing.
Run one or more jobs in subshells in parallel. Output from each job is currently intermingled, but that behavior may change in the future. The number of jobs that should execute at once is controlled by the PARALLEL_JOBS environment variable, which will default to the number of CPU cores detected.
- $@ - Command to run.
All commands execute in a subshell and are unable to affect the parent's environment. SIGINT is caught and is sent to all executing commands. This helps Control-C abort everything as it should. The status code of the command is lost. This trap is removed when you use parallel::finish
.
If you intend to trap SIGINT, please set that trap up before you call parallel::run
and only change it again after parallel::finish
.
Examples
# Sample function
sleepABit() {
echo "About to sleep for $1 seconds"
sleep "$1"
echo "Done sleeping $1 seconds"
}
# Explicitly set a limit for this example
PARALLEL_JOBS=1
# Run a job. This executes in the background.
parallel::run sleepABit 5
# Run a third job. This stalls until any previous jobs finishes.
parallel::run sleepABit 3
# Wait for all jobs to be complete
parallel::finish
# At this point, all jobs have finished.
Returns nothing.
Waits for at least one job to complete. Used internally a lot. A user of this library would be better served by looking at parallel::finish
.
Examples
parallel::run sleep 10
parallel::wait
Returns nothing.
This project is placed under an MIT License.