使用 Python 将带有数字参数的作业提交给 QSUB

Submitting jobs with numeric arguments to QSUB using Python

我想 运行 python 函数,比如 my_fun(x1,x2),在带有 SGE (QSUB) 的集群的不同节点上。我创建了一个脚本 my_script.py,它从命令行接收数字参数,所以当 运行 在本地时,我将其称为

python my_script.py x1 x2

现在我想循环使用不同的 x1 和 x2 值将此脚本提交到集群。此外,为了使节点能够访问 python 和已安装的模块,我需要在通过 QSUB 调用 python 脚本之前在节点上 运行 module load Python/2.7

这似乎是一个非常简单和典型的用例,但我无法从 Python 中找到任何直接的方法来执行此操作。在 BASH 和 Python 之间来回切换似乎有点笨拙。

我建议您根据您拥有的节点数将作业分成多个独立的作业。

对于每个 node/core,创建一个文件夹,其中包含一个包含此子作业应处理的参数列表的文件。然后在 python 中编写一个脚本来读取文件并调用您的脚本(可能使用 multiprocessing 模块来支持多核)。

编辑:

如果你想通过 q​​sub 传递额外的参数,你可以使用可以传递给你的脚本的参数调用 qsub:

qsub -F "myarg1 myarg2 myarg3=myarg3value" myscript.sh

您可以找到此文档here

这或多或少符合我的要求:

https://gist.github.com/timflutre/a9085660271bd059f71c

import sys
import subprocess

job_param1 = 12.5
job_param2 = 5.0
jobName = "python my_script.py %f %f" % (job_param1,job_param2)
cmd = "module load Python/2.7; sleep 0.2; %s" % jobName
echoArgs = ["echo", "-e", "'%s'" % cmd]
print(" ".join(echoArgs))
qsubArgs = ["qsub","-cwd"]
print(" ".join(qsubArgs))

wholeCmd = " ".join(echoArgs) + " | " + " ".join(qsubArgs)
out = subprocess.Popen(wholeCmd, shell=True, stdout=subprocess.PIPE)
out = out.communicate()[0]

jobId = out.split()[2]
print jobId