无法使用 GNU Parallel 启动作业

Can't start jobs with GNU Parallel

我是 运行 一台 32 核机器,我希望并行化一个非常简单的操作。 给定一个 ip_addresses.txt 文件,例如:

1.2.3.4
8.8.8.8
120.120.120.120

我想使用名为 script.sh 的脚本来解析这些 IPS,该脚本将 IP 解析为各自的 ISP。它被赋予一个 IP,并输出以下内容,例如当给定 1.2.3.4 时,这很好:

echo 1.2.3.4 | ./script.sh
1.2.3.4|Google

ip_addresses.txt 包含数百万个唯一 IP,我正在考虑并行调用脚本。 所以我尝试了这个:

cat ip_addresses.txt | parallel ./script.sh

但是没有输出。我希望有 :

1.2.3.4|Google
120.120.120.120|Taiwan Academic Network

这样我可以将它们重定向到一个文件。

我的脚本如下:

#!/bin/bash
while read ip
do
  ret=$(/home/sco/twdir/product/trunk/ext/libmaxminddb-1.0.3/bin/mmdblookup --file /home/sco/twdir/product/trunk/ext/libmaxminddb-1.0.3/GeoIP2-ISP.mmdb --ip $ip isp 2>/dev/null |  grep -v '^$' | grep -v '^  Could not find' | cut -d "\"" -f 2)
  [[ $ret != "" ]] &&  echo -n "$ip|" && echo $ret;
done

我错过了什么?虽然查了教程,还是搞不定

引自并行手册页。

For each line of input GNU parallel will execute command with the line as arguments.

输入中的每一行都是脚本的命令行参数,而不是标准输入。像这样:

./script.sh 1.2.3.4

您应该重写脚本以从变量 </code> 读取参数。 </p> <pre><code>#!/bin/bash ip= ret=$(/home/sco/twdir/product/trunk/ext/libmaxminddb-1.0.3/bin/mmdblookup --file /home/sco/twdir/product/trunk/ext/libmaxminddb-1.0.3/GeoIP2-ISP.mmdb --ip $ip isp 2>/dev/null | grep -v '^$' | grep -v '^ Could not find' | cut -d "\"" -f 2) [[ $ret != "" ]] && echo -n "$ip|" && echo $ret;

或者您可以使用并行的 --pipe 选项。

$ cat ip_addresses.txt | parallel --pipe --block-size 10 ./script.sh

您的脚本从标准输入 (STDIN) 读取多行。 GNU Parallel 默认将参数放在命令行上。要使 GNU Parallel 在 STDIN 上提供输入,请使用 --pipe.

cat ip_addresses.txt | parallel --pipe ./script.sh

这将 运行 每个核心一个作业,并向每个作业传递 1 MB 的数据。但是查找地址并不是真的 CPU 难,所以你可能 运行 每个 CPU 10 个工作岗位 (1000%):

cat ip_addresses.txt | parallel -j 1000% --pipe ./script.sh

这可能会达到您的文件句柄限制,因此:

cat ip_addresses.txt |\
  parallel --pipe --block 50m --round-robin -j100 parallel --pipe -j50 ./script.sh

这将 运行 100*50 = 5000 个作业并行。

如果您不想在获得任何输出之前等待处理完整的 1 MB,您可以将其降低到 1k:

cat ip_addresses.txt | parallel -j 1000% --pipe --block-size 1k ./script.sh

cat ip_addresses.txt |\
  parallel --pipe --block 50k --round-robin -j100 parallel --pipe --block 1k -j50 ./script.sh