def run_sge(target, jobs, n=<NUM_LIT:1>, nproc=None, path='<STR_LIT:.>', delete=True, wait=True, environ=None, modules=(), **kwargs):
logging.basicConfig(level=logging.DEBUG)<EOL>if isinstance(target, types.LambdaType) and target.__name__ == "<STR_LIT>":<EOL><INDENT>raise RuntimeError("<STR_LIT>")<EOL><DEDENT>src = textwrap.dedent(inspect.getsource(target)).replace(r'<STR_LIT:">', r'<STR_LIT>')<EOL>if re.match('<STR_LIT>', src.split('<STR_LIT:\n>')[<NUM_LIT:0>]) is not None:<EOL><INDENT>raise RuntimeError(<EOL>"<STR_LIT>")<EOL><DEDENT>if not os.path.isdir(path):<EOL><INDENT>os.makedirs(path)  <EOL><DEDENT>if environ is None:<EOL><INDENT>environ = {}<EOL>keys = ("<STR_LIT>", "<STR_LIT>")<EOL>for key in keys:<EOL><INDENT>if key in os.environ.keys():<EOL><INDENT>environ[key] = os.environ[key]<EOL><DEDENT><DEDENT>if "<STR_LIT>" in environ.keys() and environ["<STR_LIT>"].strip() != "<STR_LIT>":<EOL><INDENT>environ["<STR_LIT>"] = "<STR_LIT>".format(os.getcwd(), environ["<STR_LIT>"])<EOL><DEDENT>else:<EOL><INDENT>environ["<STR_LIT>"] = os.getcwd()<EOL><DEDENT><DEDENT>cmds = []<EOL>pickleins = []<EOL>pickleouts = []<EOL>scripts = []<EOL>for i, job in enumerate(jobs):<EOL><INDENT>(fd, picklein) = tempfile.mkstemp(suffix='<STR_LIT>', prefix='<STR_LIT>', dir=path)<EOL>with os.fdopen(fd, '<STR_LIT:wb>') as fout:<EOL><INDENT>pickle.dump(job, fout)<EOL><DEDENT>pickleins.append(picklein)<EOL>pickleouts.append([])<EOL>for j in range(n):<EOL><INDENT>fd, pickleout = tempfile.mkstemp(suffix='<STR_LIT>', prefix='<STR_LIT>', dir=path)<EOL>os.close(fd)<EOL>pickleouts[-<NUM_LIT:1>].append(pickleout)<EOL><DEDENT>code = '<STR_LIT>'<EOL>code += '<STR_LIT>'<EOL>code += '<STR_LIT>'<EOL>code += '<STR_LIT>'.format(picklein)<EOL>code += '<STR_LIT>'<EOL>code += '<STR_LIT>'<EOL>for m in modules:<EOL><INDENT>code += "<STR_LIT>".format(m)<EOL><DEDENT>code += src<EOL>code += '<STR_LIT>'<EOL>code += '<STR_LIT>'.format(target.__name__, i + <NUM_LIT:1>)<EOL>code += '<STR_LIT>'.format(str(pickleouts[-<NUM_LIT:1>]))<EOL>code += '<STR_LIT>'<EOL>(fd, script) = tempfile.mkstemp(suffix='<STR_LIT>', prefix='<STR_LIT>', dir=path, text=True)<EOL>with os.fdopen(fd, '<STR_LIT:w>') as fout:<EOL><INDENT>fout.write(code)<EOL><DEDENT>scripts.append(script)<EOL>cmd = '<STR_LIT>'<EOL>for key, value in environ.items():<EOL><INDENT>cmd += '<STR_LIT>'.format(key, value)<EOL><DEDENT>cmd += '<STR_LIT>'.format(script)  <EOL>cmds.append(cmd)<EOL><DEDENT>if isinstance(wait, bool):<EOL><INDENT>sync = <NUM_LIT:0> if not wait else <NUM_LIT:10><EOL><DEDENT>elif isinstance(wait, int):<EOL><INDENT>sync = wait<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>jobids = sge.run(cmds, n=n, path=path, delete=delete, sync=sync, max_running_tasks=nproc, **kwargs)<EOL>if not (sync > <NUM_LIT:0>):<EOL><INDENT>return None<EOL><DEDENT>for jobid, name in jobids:<EOL><INDENT>outputs = sge.collect(jobid, name, n=n, path=path, delete=delete)<EOL>for output in outputs:<EOL><INDENT>print(output, end='<STR_LIT>')<EOL><DEDENT><DEDENT>retval = [[pickle.load(open(pickleout, '<STR_LIT:rb>')) for pickleout in tasks]<EOL>for tasks in pickleouts]<EOL>if delete:<EOL><INDENT>for tmpname in itertools.chain(pickleins, scripts, *pickleouts):<EOL><INDENT>os.remove(tmpname)<EOL><DEDENT><DEDENT>return retval<EOL>
Evaluate the given function with each set of arguments, and return a list of results.
This function does in parallel on the Sun Grid Engine einvironment.

Parameters
----------
target : function
    A function to be evaluated. The function must accepts three arguments,
    which are a list of arguments given as `jobs`, a job and task id (int).
    This function can not be a lambda.
jobs : list
    A list of arguments passed to the function.
    All the argument must be picklable.
n : int, optional
    A number of tasks. Repeat the evaluation `n` times for each job.
    1 for default.
nproc : int, optional
    A number of cores available once.
    If nothing is given, it runs with no limit.
path : str, optional
    A path for temporary files to be saved. The path is created if not exists.
    The current directory is used as its default.
delete : bool, optional
    Whether it removes temporary files after the successful execution.
    True for default.
wait : bool, optional
    Whether it waits until all jobs are finished. If False, it just submits jobs.
    True for default.
environ : dict, optional
    An environment variables used when running jobs.
    "PYTHONPATH" and "LD_LIBRARY_PATH" is inherited when no `environ` is given.
modules : list, optional
    A list of module names imported before evaluating the given function.
    The modules are loaded as: `from [module] import *`.

Returns
-------
results : list
    A list of results. Each element is a list containing `n` results.

Examples
--------
>>> jobs = ((1, 'spam'), (2, 'ham'), (3, 'eggs'))

>>> def target(args, job_id, task_id):
...     return (args[1] * args[0])
...
>>> run_sge(target, jobs, nproc=2, path='.tmp')
[['spam'], ['hamham'], ['eggseggseggs']]

>>> def target(args, job_id, task_id):
...     return "{:d} {}".format(task_id, args[1] * args[0])
...
>>> run_sge(target, jobs, n=2, nproc=2, path='.tmp')
[['1 spam', '2 spam'], ['1 hamham', '2 hamham'], ['1 eggseggseggs', '2 eggseggseggs']]

See Also
--------
ecell4.extra.ensemble.run_serial
ecell4.extra.ensemble.run_sge
ecell4.extra.ensemble.run_slurm
ecell4.extra.ensemble.run_multiprocessing
ecell4.extra.ensemble.run_azure