Skip to content

Instantly share code, notes, and snippets.

@ohnishiakira
Created May 28, 2014 15:29
Show Gist options
  • Save ohnishiakira/cfbf0663be7f743754f8 to your computer and use it in GitHub Desktop.
Save ohnishiakira/cfbf0663be7f743754f8 to your computer and use it in GitHub Desktop.
とにかく並列で処理したい
parallel=
xargs_type=
para_func=
function find_parallel() {
echo test | gparallel --gnu echo >/dev/null 2&>/dev/null && parallel="gparallel --gnu" && para_func="_parallel"
if [[ -z "$parallel" ]]; then
echo test | parallel --gnu echo >/dev/null 2&>/dev/null && parallel="parallel --gnu" && para_func="_parallel"
fi
if [[ -z "$parallel" ]]; then
xargs --version >/dev/null 2&>/dev/null && para_func="_xargs" && xargs_type="GNU"
if [[ -z "$xargs_type" ]]; then
which xargs >/dev/null 2&>/dev/null && para_func="_xargs" && xargs_type="BSD"
fi
fi
if [[ -z "$para_func" ]]; then
para_func="serial"
fi
}
function _parallel() {
$parallel -j 4 {} ::: $@
}
function _xargs() {
if [[ "$xargs_type" = "GNU" ]]; then
echo $@ | xargs -d ' ' -I {} -n 4 bash -c {}
else
echo $@ | xargs -I {} -n 4 -P 4 bash -c {}
fi
}
function serial() {
for func in $@
do
$func
done
}
find_parallel
#!/bin/bash
souce find_parallel.sh
function test_func_1() {
echo "call test_func_1"
}
function test_func_2() {
echo "call test_func_2"
}
testcases=$(cat $0 | grep '^function test_func_' | sed 's/^.*\(test_func_[0-9a-zA-Z_]*\)(.*/\1/g')
for testcase in $testcases; do export -f $testcase; done
$para_func $testcases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment