Ok, I ran POV-Ray in all demos, but POV is still single-threaded and has not used more than one core. So, I started thinking about a solution in BASH.
I wrote a general function that takes a list of commands and runs them in the specified number of sub-shells. This really works, but I don't like the way it processes the following command in the thread-safe multi-process way :
- As an argument, a command file is required (1 per line),
- To get the "next" command, each process ("thread") will be:
- Waits until it can create a lock file using: ln $ CMDFILE $ LOCKFILE
- Read the command from the file,
- Modifies $ CMDFILE by deleting the first line,
- Removes $ LOCKFILE.
Is there a cleaner way to do this? I could not get the sub-shells to correctly read one line from FIFO.
By the way, the point is to improve what I can do on the BASH command line , and not find solutions not bash. I try to perform many complex tasks from the command line and want the tool to have another tool.Meanwhile, here is a function that handles getting the next line from a file. As you can see, it modifies the file on disk every time it reads / deletes a line. This is what seems hacky, but I did not come up with anything better, since FIFO did not work without setvbuf () in BASH.
parallel__nextLine()
{
local line rest file=$1 lock=$2
until ln "${file}" "${lock}" 2>/dev/null
do sleep 1
[ -s "${file}" ] || return $?
done
exec 3<"$file"
read line <&3 ; rest=$(cat<&3)
exec 3<&-
( [ -z "$rest" ] || echo "$rest" ) > "${file}"
rm -f "${lock}"
[ -n "$line" ] && echo "$line"
}