Running the following command does what I want when reading from a file:
parallel --gnu -j2 "echo {} && sleep 5" < myfile.txt
I would like to do something similar with a pipe. Note that I used the following page for inspiration for the pipe reader and writer: http://www.linuxjournal.com/content/using-named-pipes-fifos-bash
Here is my pipe reader file:
#!/bin/bash
pipe=/tmp/testpipe
trap "rm -f $pipe" EXIT
if [[ ! -p $pipe ]]; then
mkfifo $pipe
fi
while true
do
parallel --gnu -j2 "echo {} && sleep 5" <$pipe
done
echo "Reader exiting"
And here is my writer:
#!/bin/bash
pipe=/tmp/testpipe
if [[ ! -p $pipe ]]; then
echo "Reader not running"
exit 1
fi
if [[ "$1" ]]; then
echo "$1" >$pipe
else
echo "Hello from $$" >$pipe
fi
I then run pipeWriter several times, such as
$ ./pipeWriter one
$ ./pipeWriter two
$ ./pipeWriter three
$ ./pipeWriter four
$ ./pipeWriter five
$ ./pipeWriter six
$ ./pipeWriter seven
$ ./pipeWriter eight
$ ./pipeWriter nine
$ ./pipeWriter ten
$ ./pipeWriter ww
$ ./pipeWriter wwdfsdf
$ ./pipeWriter wwdfsdfsddfsd
$ ./pipeWriter testAgain
The shell running pipeReader shows:
$ ./pipeReader
one
four
eight
ww
testAgain
First, there is a problem of missing data. Second, parallel does not seem to run in parallel when in the pipe. I would like it to run two jobs at a time (or rather, at most two jobs. If it only has one job that's fine and if another it can start it).
Where am I going wrong?