예제 #1
0
def run_parallel_async(graph, nprocs=None, sleep=0.2):
    if nprocs == 1:
        return run_async(graph)

    nprocs = nprocs or mp.cpu_count() // 2

    with mp.Manager() as manager:
        graph = tgraph.create_parallel_compatible_graph(graph, manager)

        ioq = mp.Queue(len(graph.funcs.keys()))
        cpuq = mp.Queue(len(graph.funcs.keys()))

        for _ in range(nprocs):
            proc = mp.Process(target=run_scheduler,
                              args=(graph, sleep, ioq, cpuq))
            proc.start()

        while not tgraph.all_done(graph):
            for task in tgraph.get_ready_tasks(graph):
                graph = tgraph.mark_as_in_progress(graph, task)
                mlog(graph).info('pid {}: queueing task {}'.format(
                    os.getpid(), task))
                if task in graph.io_bound:
                    ioq.put(task)
                else:
                    cpuq.put(task)

            time.sleep(sleep)

        return tgraph.recover_values_from_manager(graph)
예제 #2
0
def run_parallel(graph, nprocs=None, sleep=0.2, raise_errors=False):
    nprocs = nprocs or mp.cpu_count() - 1
    with mp.Manager() as manager:
        graph = tgraph.create_parallel_compatible_graph(graph, manager)
        with mp.Pool(nprocs) as pool:

            exception_q = mp.Queue(10)

            def error_callback(exception):
                exception_q.put_nowait(exception)
                pool.terminate()

            while not tgraph.all_done(graph):
                for task in tgraph.get_ready_tasks(graph, reverse=False):
                    graph = tgraph.mark_as_in_progress(graph, task)
                    mlog(graph).info('pid {}: assigning task {}'.format(
                        os.getpid(), task))
                    pool.apply_async(run_task,
                                     args=(graph, task, raise_errors),
                                     error_callback=error_callback)
                time.sleep(sleep)

                if not exception_q.empty():
                    raise exception_q.get()

        return tgraph.recover_values_from_manager(graph)
예제 #3
0
def run_parallel_async(graph, nprocs=None, sleep=0.2, raise_errors=False):
    if nprocs == 1:
        return run_async(graph, sleep=sleep, raise_errors=raise_errors)

    nprocs = nprocs or mp.cpu_count() // 2

    with mp.Manager() as manager:
        graph = tgraph.create_parallel_compatible_graph(graph, manager)

        ioq = mp.Queue(len(graph.funcs.keys()))
        cpuq = mp.Queue(len(graph.funcs.keys()))

        procs = [mp.Process(target=run_scheduler,
                            args=(graph, sleep, ioq, cpuq, raise_errors))
                 for _ in range(nprocs)]
        for proc in procs:
            proc.start()

        while not tgraph.all_done(graph):
            for task in tgraph.get_ready_tasks(graph):
                graph = tgraph.mark_as_in_progress(graph, task)
                mlog(graph).info(
                    'pid {}: queueing task {}'.format(os.getpid(), task))
                if task in graph.io_bound:
                    ioq.put(task)
                else:
                    cpuq.put(task)

            time.sleep(sleep)

            if raise_errors and sum(not p.exitcode for p in procs):
                raise RuntimeError('An async task has failed. Please check your logs')

        return tgraph.recover_values_from_manager(graph)
예제 #4
0
def run_parallel(graph, nprocs=None, sleep=0.2):
    nprocs = nprocs or mp.cpu_count() - 1
    with mp.Manager() as manager:
        graph = tgraph.create_parallel_compatible_graph(graph, manager)
        with mp.Pool(nprocs) as pool:
            while not tgraph.all_done(graph):
                for task in tgraph.get_ready_tasks(graph, reverse=False):
                    graph = tgraph.mark_as_in_progress(graph, task)
                    mlog(graph).info('pid {}: assigning task {}'.format(
                        os.getpid(), task))
                    pool.apply_async(run_task, args=(graph, task))
                time.sleep(sleep)
        return tgraph.recover_values_from_manager(graph)