如何使用 xargs max-procs 实用程序并行多次调用 while 语句

How to invoke while statement multiple times in parallel using xargs max-procs utility

我在 Linux 中有一个名为 test.txt 的文件。内容如下

test.txt

['table1']
['table2']
['table3']
['table4']
['table5']
and so on

现在我有一个 while 语句循环遍历 test.txt 文件做一些工作。

While声明如下:

while read -r line; do
  table=${line:2:-2}
  validateTable=$(hive --database history -e "SHOW TABLES LIKE '$table'")
  if [[ -z $validateTable ]]; then
      /home/"$USER"/import.py "${table}"
  else
       /home/"$USER"/append.py "${table}"
  fi
done < < test.txt

在这个while语句中validateTable是检查table是否出现在hive中。

如果不存在则它将调用 import.py 脚本

如果存在,它将调用 append.py 脚本。

现在 while 语句可以正常工作了。我得到了预期的结果。

要求:

我需要的是我想并行调用 while statement。我的意思是我希望同时 while 到 运行 10 次。

最好的解决方案是什么。

我发现我们可以使用 xargs --max-procs 选项来做到这一点,但不知道如何使用它。

xargs 需要单个命令到 运行,这意味着您需要将循环主体放入 shell 脚本中。类似于(称之为 myscript

#!/bin/bash
table=${1:2:-2}
validateTable=$(hive --database history -e "SHOW TABLES LIKE '$table'")
if [[ -z $validateTable ]]; then
  /home/"$USER"/import.py "${table}"
else
  /home/"$USER"/append.py "${table}"
fi

然后运行类似

xargs --max-procs 10 myscript < test.txt