import re
import subprocess

_jobid_pat = re.compile('Your job (?P<jobid>\d+) \("')

def submit_job(scheduler_args, command):
    """Submit a job to the scheduler, returning the supplied job ID.
    """
    cl = ["qsub", "-cwd", "-b", "y", "-j", "y"] + scheduler_args + command
    status = subprocess.check_output(cl)
    match = _jobid_pat.search(status)
    return match.groups("jobid")[0]

def stop_job(jobid):
    cl = ["qdel", jobid]
    subprocess.check_call(cl)

def are_running(jobids):
    """Check if submitted job IDs are running.
    """
    run_info = subprocess.check_output(["qstat"])
    running = []
    for parts in (l.split() for l in run_info.split("\n") if l.strip()):
        if len(parts) >= 5:
            pid, _, _, _, status = parts[:5]
            if status.lower() in ["r"]:
                running.append(pid)
    want_running = set(running).intersection(set(jobids))
    return len(want_running) == len(jobids)

import re
import time
import subprocess

_jobid_pat = re.compile('Your job (?P<jobid>\d+) \("')

def submit_job(scheduler_args, command):
    """Submit a job to the scheduler, returning the supplied job ID.
    """
    cl = ["qsub", "-cwd", "-b", "y", "-j", "y"] + scheduler_args + command
    status = subprocess.check_output(cl)
    match = _jobid_pat.search(status)
    return match.groups("jobid")[0]

def stop_job(jobid):
    cl = ["qdel", jobid]
    subprocess.check_call(cl)

def are_running(jobids):
    """Check if submitted job IDs are running.
    """
    # handle SGE errors, retrying to get the current status
    max_retries = 10
    tried = 0
    while 1:
        try:
            run_info = subprocess.check_output(["qstat"])
            break
        except:
            tried += 1
            if tried > max_retries:
                raise
            time.sleep(5)
    running = []
    for parts in (l.split() for l in run_info.split("\n") if l.strip()):
        if len(parts) >= 5:
            pid, _, _, _, status = parts[:5]
            if status.lower() in ["r"]:
                running.append(pid)
    want_running = set(running).intersection(set(jobids))
    return len(want_running) == len(jobids)

Handle temporary errors returned from SGE qstat
Handle temporary errors returned from SGE qstat

  import re
+ import time
  import subprocess
  
  _jobid_pat = re.compile('Your job (?P<jobid>\d+) \("')
  
  def submit_job(scheduler_args, command):
      """Submit a job to the scheduler, returning the supplied job ID.
      """
      cl = ["qsub", "-cwd", "-b", "y", "-j", "y"] + scheduler_args + command
      status = subprocess.check_output(cl)
      match = _jobid_pat.search(status)
      return match.groups("jobid")[0]
  
  def stop_job(jobid):
      cl = ["qdel", jobid]
      subprocess.check_call(cl)
  
  def are_running(jobids):
      """Check if submitted job IDs are running.
      """
+     # handle SGE errors, retrying to get the current status
+     max_retries = 10
+     tried = 0
+     while 1:
+         try:
-     run_info = subprocess.check_output(["qstat"])
+             run_info = subprocess.check_output(["qstat"])
+             break
+         except:
+             tried += 1
+             if tried > max_retries:
+                 raise
+             time.sleep(5)
      running = []
      for parts in (l.split() for l in run_info.split("\n") if l.strip()):
          if len(parts) >= 5:
              pid, _, _, _, status = parts[:5]
              if status.lower() in ["r"]:
                  running.append(pid)
      want_running = set(running).intersection(set(jobids))
      return len(want_running) == len(jobids)
  
Handle temporary errors returned from SGE qstat
## Code Before:
import re
import subprocess

_jobid_pat = re.compile('Your job (?P<jobid>\d+) \("')

def submit_job(scheduler_args, command):
    """Submit a job to the scheduler, returning the supplied job ID.
    """
    cl = ["qsub", "-cwd", "-b", "y", "-j", "y"] + scheduler_args + command
    status = subprocess.check_output(cl)
    match = _jobid_pat.search(status)
    return match.groups("jobid")[0]

def stop_job(jobid):
    cl = ["qdel", jobid]
    subprocess.check_call(cl)

def are_running(jobids):
    """Check if submitted job IDs are running.
    """
    run_info = subprocess.check_output(["qstat"])
    running = []
    for parts in (l.split() for l in run_info.split("\n") if l.strip()):
        if len(parts) >= 5:
            pid, _, _, _, status = parts[:5]
            if status.lower() in ["r"]:
                running.append(pid)
    want_running = set(running).intersection(set(jobids))
    return len(want_running) == len(jobids)

## Instruction:
Handle temporary errors returned from SGE qstat
## Code After:
import re
import time
import subprocess

_jobid_pat = re.compile('Your job (?P<jobid>\d+) \("')

def submit_job(scheduler_args, command):
    """Submit a job to the scheduler, returning the supplied job ID.
    """
    cl = ["qsub", "-cwd", "-b", "y", "-j", "y"] + scheduler_args + command
    status = subprocess.check_output(cl)
    match = _jobid_pat.search(status)
    return match.groups("jobid")[0]

def stop_job(jobid):
    cl = ["qdel", jobid]
    subprocess.check_call(cl)

def are_running(jobids):
    """Check if submitted job IDs are running.
    """
    # handle SGE errors, retrying to get the current status
    max_retries = 10
    tried = 0
    while 1:
        try:
            run_info = subprocess.check_output(["qstat"])
            break
        except:
            tried += 1
            if tried > max_retries:
                raise
            time.sleep(5)
    running = []
    for parts in (l.split() for l in run_info.split("\n") if l.strip()):
        if len(parts) >= 5:
            pid, _, _, _, status = parts[:5]
            if status.lower() in ["r"]:
                running.append(pid)
    want_running = set(running).intersection(set(jobids))
    return len(want_running) == len(jobids)
