def run_safe_jobs( directory, jobs, processes=None):
    """In the event that Grid Engine is not installed, this program will
    run jobs serially on the local host."""
    #pool = Pool(processes=max(cpu_count(), 1))
    pool = Pool(processes=processes)
    errorCodes = []
    for job in jobs:
        job.out = os.path.join( directory, "stdout" )
        job.err = os.path.join( directory, "stderr" )

        commands = []
        if isinstance( job, JobGroup ):
            for task in range(1,job.tasks+1):
                command = "export SGE_TASK_ID=%d; %s" % (task, job.scriptPath)
                commands.append(command)
        else:
            commands.append(job.scriptPath)

        count = 0
        for command in commands:
            print "# %s" % (command)
            #command += " 2>%s/%s.%d >%s/%s.%d" % (job.err, job.name, count, job.out, job.name, count)
            #os.system(command)
            print 'the command is', command
            errorCode = pool.apply_async(os.system, (command,))
            errorCodes.append(errorCode)
            count += 1
        job.submitted = 1
    # wait for submitted jobs to finish
    pool.close()
    pool.join()
    # make sure all the jobs were successful
    for index, code in enumerate(errorCodes):
        print index, code
        try:
            code.successful()
        except __HOLE__, e:
            raise AssertionError("Job Failed to run: %s  %s" % (jobs[index], commands[index]), e.args)
dataset/ETHPy150Open uci-cbcl/tree-hmm/treehmm/sge.py/run_safe_jobs