bash - 在 bash 中进行并行处理?

标签 bash parallel-processing

我有数以千计的 png 文件,我想使用 pngcrush 将它们缩小。我有一个简单的 find .. -exec 作业,但它是顺序的。我的机器有很多资源,我会并行处理。

对每个png要执行的操作是:

pngcrush input output && mv output input

理想情况下,我可以指定并行操作的最大数量。

有没有办法用 bash 和/或其他 shell 助手来做到这一点?我是 Ubuntu 或 Debian。

最佳答案

您可以使用 xargs并行运行多个进程:

find /path -print0 | xargs -0 -n 1 -P <nr_procs> sh -c 'pngcrush $1 temp.$$ && mv temp.$$ $1' sh

xargs将读取由 find 生成的文件列表(由 0 个字符分隔(-0))并一次使用一个参数(sh -c '...' sh)运行提供的命令(-n 1)。 xargs 将运行 <nr_procs> ( -P <nr_procs> ) 并行。

关于bash - 在 bash 中进行并行处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57312894/

相关文章:

haskell - Haskell 中工作池并行运行命令调用的超时

Ruby 线程池在 ThreadError.queue _empty 上暂停

linux - 使用 awk 或 sed 基于公共(public)列组合两个 csv 文件

bash - 为什么 shell 会忽略通过变量传递给它的参数中的引号字符?

.net - 处理服务中传入请求的体系结构

scala - Scala 中的并行迭代器

selenium - 使用gradle,testNG不能高效地并行调度测试

linux - 在 bash 脚本中搜索子字符串将不起作用

linux - 如何使用脚本自动打开提示、ssh 和做事?

bash - test -L 命令在从提示符调用时返回的错误代码与从脚本内部调用时返回的错误代码不同