Executing bash convert commands asynchronously

笑着哭i 提交于 2019-12-08 06:43:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!