python - 使用 GNU parallel 并行化多线程命令

标签 python multithreading gnu-parallel

我刚刚写了一个涉及多线程的 python 脚本,比如:

    python myScript.py -cpu_n 5 -i input_file

为了对我的数百个输入文件运行命令,我为每个文件生成一个命令列表 (commands.list):

    python myScript.py -cpu_n 5 -i input_file1
    python myScript.py -cpu_n 5 -i input_file2
    python myScript.py -cpu_n 5 -i input_file3
    ...

我正在尝试使用“并行”命令和三台不同机器的 10 个 CPU 来安排它们:

   parallel -S 10/$server1 -S 10/$server2 -S 10/$server3 < commands.list

我的问题是:使用并行命令在每台服务器上使用的最大 CPU 数量是多少?是 5*10=50 还是 10 个 CPU?

最佳答案

来自 man parallel:

   --jobs N
   -j N
   --max-procs N
   -P N     Number of jobslots on each machine. Run up to N
            jobs in parallel.  0 means as many as possible.
            Default is 100% which will run one job per CPU
            core on each machine.


   -S
   [@hostgroups/][ncpu/]sshlogin[,[@hostgroups/][ncpu/]sshlogin[,...]]
   :
            GNU parallel will determine the number of CPU
            cores on the remote computers and run the number
            of jobs as specified by -j.  If the number ncpu
            is given GNU parallel will use this number for
            number of CPU cores on the host. Normally ncpu
            will not be needed.

因此您的命令将在每台服务器上并行运行多达 10 个作业。

您的每个命令是否会使用 5 个 CPU 内核尚不清楚。如果您的每个命令都使用 5 个内核,则每个服务器将使用 50 个内核,在这种情况下,我建议您不要使用 ncpu/server 语法,而是使用:

parallel -j 20% -S $server1,$server2,$server3 < commands.list

这样您就可以混合使用具有不同内核数的服务器,GNU Parallel 将并行启动其中的 1/5。

关于python - 使用 GNU parallel 并行化多线程命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41870486/

相关文章:

python - 使用机器人框架在谷歌翻译中点击 Selenium 按钮

java - 在 Java 中创建三个线程来计算三个不同的项目

bash - 使用 GNU parallel 拆分命令行参数

python - 创建一个数据框,包括分组总和和总和

python - Spark MapPartitions

python - 是否有更惯用的方法来使用 Python 的 Click 获取参数列表?

c++ - 来自 TBB 节点的异步输入/输出和非均匀输出

c# - 线程挂起,直到我附加调试器

linux - 加快文件从一台机器传输到另一台机器

linux - 如何安装或切换到旧版本的 GNU Parallel?