Exemplo n.º 1
0
    def result(self, timeout=None):
        """
        Set all asynchronous results.

        :param timeout:
            The number of seconds to wait for the result if the futures aren't
            done. If None, then there is no limit on the wait time.
        :type timeout: float

        :return:
            Update Solution.
        :rtype: Solution
        """
        futs, ex = collections.OrderedDict(), False

        def _update(fut, data, key):
            if isinstance(fut, Future):
                get_nested_dicts(futs, fut, default=list).append((data, key))
            elif isinstance(fut, AsyncList):
                for i, j in enumerate(fut):
                    if isinstance(j, Future):
                        get_nested_dicts(futs, j, default=list).append((fut, i))

        for p in self._pipe:
            n, s = p[-1]
            if n in s:
                _update(s[n], s, n)

        for sol in self.sub_sol.values():
            for k, f in sol.items():
                _update(f, sol, k)

            for attr in sol.workflow.nodes.values():
                if 'results' in attr:
                    _update(attr['results'], attr, 'results')

            for attr in sol.workflow.edges.values():
                if 'value' in attr:
                    _update(attr['value'], attr, 'value')
        if futs:
            from concurrent.futures import wait as wait_fut
            wait_fut(futs, timeout)
        EXECUTORS.set_active(id(self), False)
        exceptions = Exception, ExecutorShutdown, DispatcherAbort, SkipNode
        for f, it in futs.items():
            try:
                r = await_result(f, 0)
                for d, k in it:
                    d[k] = r
            except exceptions as e:
                for d, k in it:
                    if k in d:
                        del d[k]
                if not ex:
                    ex = isinstance(e, SkipNode) and e.ex or e
        if ex:
            raise ex
        return self
Exemplo n.º 2
0
    def result(self, timeout=None):
        """
        Set all asynchronous results.

        :param timeout:
            The number of seconds to wait for the result if the futures aren't
            done. If None, then there is no limit on the wait time.
        :type timeout: float

        :return:
            Update Solution.
        :rtype: Solution
        """
        it, exceptions, future_lists = [], [], []
        from concurrent.futures import Future, wait as wait_fut

        def update(fut, data, key):
            if isinstance(fut, Future):
                it.append((fut, data, key))
            elif isinstance(fut, AsyncList) and fut not in future_lists:
                future_lists.append(fut)
                it.extend([(j, fut, i)
                           for i, j in enumerate(fut)
                           if isinstance(j, Future)][::-1])

        for s in self.sub_sol.values():
            for k, v in list(s.items()):
                update(v, s, k)

            for d in s.workflow.nodes.values():
                if 'results' in d:
                    update(d['results'], d, 'results')

            for d in s.workflow.edges.values():
                if 'value' in d:
                    update(d['value'], d, 'value')

        wait_fut({v[0] for v in it}, timeout)

        for f, d, k in it:
            try:
                d[k] = await_result(f, 0)
            except SkipNode as e:
                exceptions.append((f, d, k, e.ex))
                del d[k]
            except (Exception, ExecutorShutdown, DispatcherAbort) as ex:
                exceptions.append((f, d, k, ex))
                del d[k]

        if exceptions:
            raise exceptions[0][-1]
        return self
Exemplo n.º 3
0
    def shutdown(self, wait=True):
        from .exc import ExecutorShutdown
        from concurrent.futures import wait as wait_fut
        tasks = dict(self.tasks)
        if wait:
            wait_fut(tasks)

        for fut, task in tasks.items():
            not fut.done() and fut.set_exception(ExecutorShutdown)
            try:
                task.terminate()
            except AttributeError:
                pass
        return tasks
Exemplo n.º 4
0
    def shutdown(self, wait=True):
        from .exc import ExecutorShutdown
        from concurrent.futures import wait as wait_fut
        tasks = dict(self.tasks)
        if wait:
            wait_fut(tasks)

        for fut, task in tasks.items():
            not fut.done() and fut.set_exception(ExecutorShutdown)
            try:
                task.terminate()
            except AttributeError:
                pass
        return tasks