异步执行bash转换命令 [英] Executing bash convert commands asynchronously

查看:209
本文介绍了异步执行bash转换命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环遍历一堆我需要用Imagemagick编辑的图像。麻烦的是,在编辑下一个图像之前必须等待编辑每个图像很慢。我想异步执行每个转换命令而不是等待最后一个完成。这可能与bash一起使用吗?以下是我所拥有的基本概述:

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

我想异步执行转换命令。所以我可以一次转换所有图像。用bash可以实现这个吗?

I want to execute that convert command asynchronously. So I can convert all the images at once. Is this possible with bash?

推荐答案

如果你有很多图像,运行数百/数千转换并行进程不能很好地工作,你最好使用 GNU Parallel ,它可以让你的所有CPU内核保持忙碌而不会使系统过载 - 所以如果你有8个核心,它一次会做8个图像(虽然你可以改变它)。

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).

所以,如果你想调整所有 JPG 目录中的图片缩小到原始尺寸的一半并重命名为 resized-XYZ.jpg

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

如果你想做所有 JPG 文件和 PNG 文件,看看他们运行的进度表:

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

如果你想特别做8一次,使用:

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 ...

这篇关于异步执行bash转换命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆