Hacker News new | ask | show | jobs
by termie 3191 days ago
Bash’s built-in wait is also handy when you want quick and simple parallelism. http://tldp.org/LDP/abs/html/x9644.html
1 comments

I do this pattern a lot

    for x in $(seq 1 10); do long_process file${x} & done; wait
It doesn't take care of not saturating my cpu or anything; when I need to care about that then I try to remember how to use parallel.
it's actually easier using parallel :

        for x in ...; do echo long_process file$x ; done | parallel -j 8

EDIT: and in your case, it is even easier with xargs, e.g.:

        ls files* | xargs -n 1 -P 8 long_process
Wouldn't this fail if your file names have white space?
Parsing `ls` is never a good idea[1]. A more robust way to use globbing in a POSIX shell would be something like this:

    printf "%s\0" files* | xargs -0 -n 1 -P 8 long_process
1. http://mywiki.wooledge.org/ParsingLs
I disagree with the article that you linked. Filenames are variable names that you get to choose. Half of the game is won by chosing them wisely, so that they are convenient to use.
I use this template to deal with reasonable filenames:

    find ... -print0|parallel --gnu -X -0 -n 1 your_command "{}" \;
And sometimes the names are so screwed that they have to be renamed first, then this can give you a reasonable new name

    $(echo $(basename "{}")|tr '[[:blank:]]' '_'| tr -cd '\/.[[:alnum:]]_-' )
yes, and rightfully so. If you don't use silly filenames you can write simpler scripts.
You don't always have the luxury of choosing the names that the files have.
I actually do.

In the rare cases where I have to work with odd filenames, it is easier to rename those files than to change my clean scripts.

Ahh, I see a sysadmin with users! :)
I would do:

    seq 10 | parallel long_process file{}
Or simply:

    parallel long_process ::: file*