mainparallel - build and execute command lines from standard input in parallel - Summary

 
 
Show feedback again
Membership Info
Project Admin:
1 active member

Group identification
Id: #9478
System Name: parallel
Name: parallel - build and execute command lines from standard input in parallel
Group Type: non-GNU software & documentation

Search in this Group

in
   

This project is not part of the GNU Project.

NAME

parallel - build and execute command lines from standard input in parallel

SYNOPSIS

parallel [-0cfgkqsuvxX] [-j num] [command [arguments]] < list_of_arguments

DESCRIPTION

For each line of input parallel will execute command with the line as arguments. If no command is given the line of input is executed. parallel can often be used as a substitute for xargs or cat | sh.

Several lines will be run in parallel.

  • command

Command to execute. If command or the following arguments contain {} every instance will be substituted with the input line. Setting a command also invokes -f.

If command is given, parallel will behave similar to xargs. If command is not given parallel will behave similar to cat | sh.

  • -0

Use NUL as delimiter. Normally input lines will end in \n (newline). If they end in \0 (NUL), then use this option. It is useful for processing filenames that may contain \n (newline).

  • -c

Line is a command. The input line contains more than one argument or the input line needs to be evaluated by the shell. This is the default if command is not set. Can be reversed with -f.

  • -f

Line is a filename. The input line contains a filename that will be quoted so it is not evaluated by the shell. This is the default if command is set. Can be reversed with -c.

  • -g

Group output. Output from each jobs is grouped together and is only printed when the command is finished. STDERR first followed by STDOUT. -g is the default. Can be reversed with -u.

  • -j N

Run N jobs in parallel. 0 means as many as possible. Default is 10.

  • -j +N

Add N to the number of CPUs. Run this many jobs in parallel. For compute intensive jobs -j +0 is useful as it will run number-of-cpus jobs in parallel.

  • -j -N

Subtract N from the number of CPUs. Run this many jobs in parallel. If the evaluated number is less than 1 then 1 will be used.

  • -j N%

Multiply N% with the number of CPUs. Run this many jobs in parallel. If the evaluated number is less than 1 then 1 will be used.

  • -k

Keep sequence of output same as the order of input. If jobs 1 2 3 4 end in the sequence 3 1 4 2 the output will still be 1 2 3 4.

  • -q

Quote command. This will quote the command line so special characters are not interpreted by the shell. See the section QUOTING. Most people will never need this. Quoting is disabled by default.

  • -s

Silent. The job to be run will not be printed. This is the default. Can be reversed with -v.

  • -u

Ungroup output. Output is printed as soon as possible. This may cause output from different commands to be mixed. Can be reversed with -g.

  • -v

Verbose. Print the job to be run. Can be reversed with -s.

  • -x

xargs. Insert as many arguments as the command line length permits. If {} is not used the arguments will be appended to the line. If {} is used multiple times each {} will be replaced with all the arguments.

  • -X

xargs with context replace. This works like -x except if {} is part of a word (like pic{}.jpg) then the whole word will be repeated.

EXAMPLE 1: Working as cat | sh. Ressource inexpensive jobs and evaluation

parallel can work similar to cat | sh.

A ressource inexpensive job is a job that takes very little CPU, disk I/O and network I/O. Ping is an example of a ressource inexpensive job. wget is too - if the webpages are small.

The content of the file jobs_to_run:

To run 100 processes simultaneously do:

parallel -j 100 < jobs_to_run

As there is not a command the option -c is default because the jobs needs to be evaluated by the shell.

EXAMPLE 2: Working as xargs -n1. Argument appending

parallel can work similar to xargs -n1.

To output all html files run:

find . -name '*.html' | parallel cat

As there is a command the option -f is default because the filenames needs to be protected from the shell in case a filename contains special characters.

EXAMPLE 3: Compute intensive jobs and substitution

If ImageMagick is installed this will generate a thumbnail of a jpg file:

convert -geometry 120 foo.jpg thumb_foo.jpg

If the system has more than 1 CPU it can be run with number-of-cpus jobs in parallel (-j +0). This will do that for all jpg files in a directory:

ls *.jpg | parallel -j +0 convert -geometry 120 {} thumb_{}

To do it recursively use find:

find . -name '*.jpg' | parallel -j +0 convert -geometry 120 {} {}_thumb.jpg

Notice how the argument has to start with {} as {} will include path (e.g. running convert -geometry 120 ./foo/bar.jpg thumb_./foo/bar.jpg would clearly be wrong). It will result in files like ./foo/bar.jpg_thumb.jpg. If that is not wanted this can fix it:

Unfortunately this will not work if the filenames contain special
characters (such as space or quotes). If you have ren installed this is a better solution:

EXAMPLE 4: Substitution and redirection

This will compare all files in the dir to the file foo and save the diffs in corresponding .diff files:

ls | parallel diff {} foo ">"{}.diff

Quoting of > is necessary to postpone the redirection. Another solution is to quote the whole command:

ls | parallel "diff {} foo >{}.diff"

EXAMPLE 5: Composed commands

A job can consist of several commands. This will print the number of files in each directory:

ls | parallel 'echo -n {}" "; ls {}|wc -l'

To put the output in a file called <name>.dir:

ls | parallel '(echo -n {}" "; ls {}|wc -l) > {}.dir'

EXAMPLE 6: Context replace

To remove the files pict0000.jpg .. pict9999.jpg you could do:

seq -f %04g 0 9999 | parallel rm pict{}.jpg

You could also do:

seq -f %04g 0 9999 | perl -pe 's/(.*)/pict$1.jpg/' | parallel -x rm

The first will run rm 10000 times, while the last will only run rm as many times needed to keep the command line length short enough (typically 1-2 times).

You could also run:

seq -f %04g 0 9999 | parallel -X rm pict{}.jpg

This will also only run rm as many times needed to keep the command line length short enough.

EXAMPLE 7: Keep order of output same as order of input

Normally the output of a job will be printed as soon as it completes. Sometimes you want the order of the output to remain the same as the order of the input. -k will make sure the order of output will be in the same order as input even if later jobs end before earlier jobs.

If you have a directory with subdirectories that contain different amount of files running:

ls | sort | parallel -v "ls {} | wc"

will give the output of each dir, but it will be sorted accoring to which job completed first.

To keep the order the same as input run:

ls | sort | parallel -kv "ls {} | wc"

QUOTING

For more advanced use quoting may be an issue. The following will print the filename for each line that has exactly 2 columns:

perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"' file

This can be done by parallel using:

ls | parallel "perl -ne '/^\\S+\\s+\\S+$/ and print \$ARGV,\"\\n\"'"

Notice how \'s, "'s, and $'s needs to be quoted. parallel can do the quoting by using option -q:

ls | parallel -q perl -ne '/^\S+\s+\S+$/ and print $ARGV,"\n"'

However, this means you cannot make the shell interpret special characters. For example this will not work:

ls | parallel -q "diff {} foo >{}.diff"

ls | parallel -q "ls {} | wc -l"

because > and | need to be interpreted by the shell.

If you get errors like:

sh: -c: line 0: syntax error near unexpected token

then you might try using -q.

If you are using bash process substitution like <(cat foo) then you may try -q and prepending command with bash -c:

ls | parallel -q bash -c 'wc -c <(echo {})'

Or for substituting output:

ls | parallel -q bash -c 'tar c {} | tee >(gzip >{}.tar.gz) | bzip2 >{}.tar.bz2'

Conclusion: To avoid dealing with the quoting problems it may be easier just to write a small script and have parallel call that script.

DIFFERENCES BETWEEN xargs/find -exec AND parallel

xargs and find -exec offer some of the same possibilites as parallel.

find -exec only works on files. So processing other input (such as hosts or URLs) will require creating these inputs as files. find -exec has no support for running commands in parallel.

xargs deals badly with special characters (such as space, ' and ") unless -0 or -d "\n" is specified. Many input generators are not optimized for using NUL as separator but are optimized for newline as separator. E.g head, tail, awk, ls, echo, sed, tar -v, perl (-0 and \0 instead of \n), locate (requires using -0), find (requires using -print0), grep (requires user to use -z or -Z).

So parallel's newline separation can be emulated with:

cat | xargs -d "\n" -n1 command

xargs can run a given number of jobs in parallel, but has no support for running no_of_cpus jobs in parallel.

xargs has no support for grouping the output, therefore output may run together, e.g. the first half of a line is from one process and the last half of the line is from another process.

xargs has no support for keeping the order of the output, therefore output of the second job cannot be postponed till the first job is done.

xargs has no support for context replace, so you will have to create the arguments.

If you use a replace string in xargs (-I) you can not force xargs to use more than one argument.

Quoting in xargs works like -q in parallel. This means composed commands and redirection requires using bash -c.

ls | parallel "wc {} > {}.wc"

becomes

ls | xargs -d "\n" -P10 -I {} bash -c "wc {} >* {}.wc"*

and

ls | parallel "echo {}; ls {}|wc"

becomes

ls | xargs -d "\n" -P10 -I {} bash -c "echo {}; ls {}|wc"

BUGS

Filenames beginning with '-' can cause some commands to give unexpected results, as it will often be interpreted as an option.

REPORTING BUGS

Report bugs to <bug-parallel@tange.dk>.

IDEAS

xargs dropin-replacement. Implement the missing --features

AUTHOR

Copyright (C) 2007-10-18 Ole Tange, http://ole.tange.dk

Copyright (C) 2008-2009 Ole Tange, http://ole.tange.dk

LICENSE

Copyright (C) 2007-2009 Free Software Foundation, Inc.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

DEPENDENCIES

parallel uses Perl, and the Perl modules Getopt::Std, IPC::Open3, Symbol, IO::File, POSIX, and File::Temp.

SEE ALSO

find(1), xargs(1)

Registration Date: Mon 27 Aug 2007 02:16:53 PM UTC
License: GNU General Public License v3 or later
Development Status: 5 - Production/Stable

 

Latest News 
Show feedback again

Back to the top


Powered by Savane 3.1-cleanup