问题
I have a loop that cycles through a bunch of images that I need to edit with Imagemagick. Trouble is, it's slow to have to wait for each image to be edited before editing the next one. I want to execute each of the convert commands asynchronously not waiting for the last one to finish.Is this possible with bash? Here's a basic overview of what I have:
for img in *
do
convert $img **more params here**
done
I want to execute that convert command asynchronously. So I can convert all the images at once. Is this possible with bash?
回答1:
If you have many images, running hundreds/thousands of convert
processes in parallel is not going to work very well and you would be better off with GNU Parallel which will keep all your CPU cores busy without overloading the system - so if you have 8 cores, it will do 8 images at a time (though you can change that).
So, if you wanted to resize all the JPG
images in your directory down to half their original size and rename them resized-XYZ.jpg
:
parallel convert {} -resize 50% resized-{} ::: *.jpg
If you want to do all the JPG
files and the PNG
files and see a progress meter as they run:
parallel --progress convert {} -resize 50% resized-{} ::: *.jpg *.png
If you want to do specifically 8 at a time, use:
parallel -j8 ....
If you want to see what the command is going to do, without actually doing anything:
parallel --dry-run ...
来源:https://stackoverflow.com/questions/42664397/executing-bash-convert-commands-asynchronously