I love concurrency. Writing a concurrent program is always complex. There are different technologies which are most suitable to do this job. In an earlier post I briefed how to achieve parallelization using PHP. Parallel processing can be done using shell script also; by spawning multiple child processes. It’s fairly simple to handle basic concurrent tasks in that manner.
A basic example to achieve the same-
#!/bin/bash declare -a NUMBERS=(1 2 3 4 5 6 7 8); functionName() { echo "pid: $BASHPID => number: $1"; } export -f functionName for NUMBER in ${NUMBERS[*]}; do echo $NUMBER; done | xargs -P4 -n1 -I{} bash -c "functionName '{}'";
Output:
pid: 9970 => number: 3 pid: 9971 => number: 4 pid: 9968 => number: 1 pid: 9969 => number: 2 pid: 9973 => number: 6 pid: 9972 => number: 5 pid: 9975 => number: 8 pid: 9974 => number: 7
Here I’ve explained a practical example.