python如何等待批处理SGE脚本完成执行?

时间:2014-11-18 16:22:28

标签: python batch-file interaction sungridengine

我有一个问题我希望你帮我解决。

我在Python工作,我想做以下事情:

  • 在服务器上调用SGE批处理脚本
  • 看看它是否正常
  • 做点什么

我现在所做的大概如下:

    import subprocess
    try:
       tmp = subprocess.call(qsub ....)
       if tmp != 0:
           error_handler_1()
       else:
           correct_routine()
    except:
       error_handler_2()

我的问题是,一旦脚本被发送到SGE,我的python脚本会将其解释为成功并继续工作,就像它完成一样。

您对如何让python代码等待SGE脚本的实际处理结果有什么建议吗?

啊,顺便说一句,我尝试使用qrsh,但我没有获准在SGE上使用它

谢谢!

2 个答案:

答案 0 :(得分:2)

从您的代码中,您希望程序等待作业完成并返回代码,对吧?如果是这样,qsub同步选项很可能是你想要的:

http://gridscheduler.sourceforge.net/htmlman/htmlman1/qsub.html

答案 1 :(得分:1)

更容易处理的附加答案: 通过使用python drmaa 模块:link,它允许使用SGE进行更完整的处理。 文档中提供的功能代码如下:[假设您将sleeper.sh脚本放在同一目录中] 请注意,执行.sh脚本需要 -b n 选项,否则默认情况下需要二进制文件,如解释here

import drmaa
import os

def main():
   """Submit a job.
   Note, need file called sleeper.sh in current directory.
   """
   s = drmaa.Session()
   s.initialize()
   print 'Creating job template'
   jt = s.createJobTemplate()
   jt.remoteCommand = os.getcwd()+'/sleeper.sh'
   jt.args = ['42','Simon says:']
   jt.joinFiles=False
   jt.nativeSpecification  ="-m abe -M mymail -q so-el6 -b n"
   jobid = s.runJob(jt)
   print 'Your job has been submitted with id ' + jobid
   retval = s.wait(jobid, drmaa.Session.TIMEOUT_WAIT_FOREVER)
   print('Job: {0} finished with status {1}'.format(retval.jobId, retval.hasExited))
   print 'Cleaning up'
   s.deleteJobTemplate(jt)
   s.exit()

if __name__=='__main__':
    main()
相关问题