Skip to content

bpm-rocks/parallel

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

BPM Library: Parallel

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.

Installation

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

API

PARALLEL_JOBS

The number of jobs to run at once. Leave blank to auto detect.

PARALLEL_JOB_LIST

Don't modify this list. It's tracking the PIDs of every job running in the background.

PARALLEL_JOB_STATUS_LIST

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.

PARALLEL_OLD_INT_TRAP

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.

PARALLEL_WAIT_METHOD

The method to use when wating for jobs to finish. wait -n is faster but only introduced in Bash 4.

parallel::curateJobList()

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.

parallel::finish()

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.

parallel::intTrap()

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.

parallel::maxJobs()

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.

parallel::run()

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.

parallel::wait()

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.

License

This project is placed under an MIT License.

About

Run multiple commands in parallel

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages