Bash Multiprocessing

For those who didn’t get that ninja feeling by now, this is for you. We’ll combine all our newly learned superpowers and perform multiprocessing, also called parallel computing, all with a single command in Bash!

Xargs

The xargs command reads items from standard input (meaning, you can pipe data to it) and executes the specified command.

The basic syntax for xargs is:

xargs [options] [command [initial-arguments]]

At first sight, you might not see the benefits of this. Why not create a while loop and run each command? The benefit of xargs is that it can batch arguments and call your command once on many files, instead of individually for each file.

But find can do so too!

Yup, you’re right. Still, there are more advantages. Xargs will work without needing find. So that’s one. But xargs has a special trick up its sleeve: it can run commands in parallel with the -P-option.

This option takes a number that defines how many processes it needs to start in parallel. You read that right — in parallel!

Example

One real-world example for Bash multiprocessing with xargs, is to use this when doing video conversion on lots of files. Let’s dissect the following command together:

$ find . -name "*.mpeg" -print0 | xargs -0 -P 4 -I {} ffmpeg -i {} {}.mp4

First, we find all mpeg files. We feed these files to xargs. The -print0 option makes find separate the file names with a null character instead of a newline, and -0 tells xargs to expect exactly that. Without this pair, xargs chokes on file names with quotes in them, like Erik's trip.mpeg, or with newlines. It’s a good habit whenever you pipe find into xargs. Next we tell xargs, with -P 4, to use four processes concurrently. We also tell xargs to substitute the file name in all places where it encounters {} with the -I option. So xargs gets the first video file and starts ffmpeg. Instead of waiting for ffmpeg to finish, xargs starts another instance of ffmpeg to process the second file in parallel. This goes on until it reaches four processes. If all four slots are taken, xargs waits for one to finish before starting the next process.

ffmpeg takes the input file after -i and the output file as the last argument, so holiday.mpeg is converted to holiday.mpeg.mp4. ffmpeg picks the output format based on that file extension.

Video conversion is mostly CPU-bound, so running several conversions at once can save a lot of time. Don’t expect it to be exactly four times as fast on four cores, though. ffmpeg already uses multiple threads for many codecs, so a single conversion may already keep several cores busy. The gain is largest for tools that use only one CPU core, or when you process many small files. Try a few values for -P and see what works best on your machine.

More info

For more info, read the man page on the command-line or on the web. Wikipedia has some examples of xargs usage as well that you might find useful.

If you do this kind of thing often, look at GNU parallel. It’s a separate tool that you need to install, but it’s built for exactly this job: it keeps the output of each job together, can show progress, and can even spread work over multiple machines.

If you prefer to work in Python, our guide contains a comprehensive section on multiprocessing, or concurrency, with Python.

Related articles

Leave a Comment