Skip to content

Instantly share code, notes, and snippets.

@kdaily
Created June 5, 2013 19:37
Show Gist options
  • Save kdaily/5716627 to your computer and use it in GitHub Desktop.
Save kdaily/5716627 to your computer and use it in GitHub Desktop.
def safe_qsub_run(cmd, jobname, script_header=_script_header, nodes=1, params="", stdout=None, stderr=None, shell=False):
"""Run a command via qsub in blocking mode so that the command waits to exit.
Requires a header string and a job name.
"""
scriptfile = tempfile.NamedTemporaryFile()
scriptfile.write("%(header)s\n%(command)s\n" % dict(header=script_header, command=cmd))
scriptfile.file.flush()
qsub_cmd = "qsub -N %(jobname)s -l nodes=%(nodes)s -W block=true %(params)s" % dict(jobname=jobname,
nodes=nodes,
params=params)
if stdout:
qsub_cmd += " -o %s" % stdout
if stderr:
qsub_cmd += " -e %s" % stderr
qsub_cmd = "%(cmd)s %(script)s" % dict(cmd=qsub_cmd, script=scriptfile.name)
proc = subprocess.Popen(shlex.split(qsub_cmd), shell=False,
stdin=subprocess.PIPE,
stderr=subprocess.STDOUT, stdout=subprocess.PIPE)
proc.stdin.close()
jobid = os.waitpid(proc.pid, 0)
# # Another way of doing it without setting stdin=subprocess.PIPE; same results
# stdouts, stderrs = proc.communicate()
# jobid = stdouts.rstrip()
scriptfile.close()
return jobid#, stderrs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment