Created
May 4, 2014 12:51
-
-
Save sebgod/fc9920741d5ddd4f36b2 to your computer and use it in GitHub Desktop.
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
active_jobs=() | |
max_jobs=4 | |
refresh_jobs() { | |
local pid | |
local actual_jobs=() | |
for pid in "${active_jobs[@]}"; do | |
kill -0 "$pid" 2> /dev/null \ | |
&& actual_jobs+=("$pid") \ | |
|| wait "$pid" # avoid zombie | |
done | |
active_jobs=("${actual_jobs[@]}") | |
} | |
trap refresh_jobs SIGCHLD | |
spawn() { | |
[ ${#active_jobs[@]} -ge $max_jobs ] && refresh_jobs | |
while [ ${#active_jobs[@]} -ge $max_jobs ]; do | |
msg "Waiting for jobs..." | |
sleep 5 | |
refresh_jobs | |
done | |
"$@" & | |
active_jobs+=($!) | |
} | |
barrier() { | |
wait "${active_jobs[@]}" | |
active_jobs=() | |
} | |
# ... | |
msg "Building DjVuDocuments" | |
for page in "$tmpdir"/page-*.p[pgb]m "$tmpdir"/page-*.jpg; do | |
case "$page" in | |
*-\*.*) continue ;; | |
esac | |
djvu="${page%.*}.djvu" | |
msg "Encode $page" | |
# cpaldjvu -colors $color -bgwhite "$page" "$djvu" || die "Failed to encode $page" | |
spawn cpaldjvu -colors $color -bgwhite "$page" "$djvu" | |
done | |
# now wait for all spawns | |
barrier |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment