Last active
May 28, 2019 18:01
-
-
Save DamnedFacts/8939c2f1f16451c5b64281a8411cda29 to your computer and use it in GitHub Desktop.
An exercise in embarassing parallelization of Bash functions, done through a file archival example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
PREFILLED=false | |
SLOTS=8 | |
END_YEAR=2099 | |
year=2018 | |
declare -a procs[8] | |
function archive { | |
test -z "${1}" && echo 'Missing year parameter!' && return | |
local year=${1} | |
echo "Zipping files for ${year}..." | |
if [[ `find Individual_Hit_Data/ -regex ".*/${year}.*" -printf '.' | wc -c` -eq 0 ]]; then | |
continue | |
fi | |
echo "time find Individual_Hit_Data/ -regex .*/${year}.* | tar jcvf ${year}-individualHits.tar.bz2 -T -" | |
echo | |
} | |
function spawn { | |
local i=${1} | |
archive ${year} & # parallelize simply by forking into the background | |
procs[${i}]=$! | |
echo "Spawing parallel job at PID $! for year ${year}" | |
year=$((${year}+1)) | |
} | |
while true; do | |
for i in $(seq 0 $((${SLOTS}-1))); do | |
if ! ${PREFILLED}; then | |
spawn ${i} | |
echo | |
if [ ${#procs[@]} -eq ${SLOTS} ]; then | |
PREFILLED=true | |
fi | |
else | |
if ! ps -p ${procs[$i]} >&-; then | |
echo "Job PID ${procs[$i]} finished..." | |
echo | |
unset procs[$i] | |
spawn ${i} | |
fi | |
fi | |
done | |
if [[ ${year} -gt ${END_YEAR} ]]; then | |
exit 0 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment