Created
June 5, 2013 19:37
-
-
Save kdaily/5716627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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