Esempio n. 1
0
    def apply(self,
              func,
              args=None,
              kwargs=None,
              targets=None,
              autoproxyize=False):
        """
        Analogous to IPython.parallel.view.apply_sync

        Parameters
        ----------
        func : function
        args : tuple
            positional arguments to func
        kwargs : dict
            keyword arguments to func
        targets : sequence of integers
            engines func is to be run on.
        autoproxyize: bool, default False
            If True, implicitly return a Proxy object from the function.

        Returns
        -------
        list
            result from each engine.
        """
        # default arguments
        args = () if args is None else args
        kwargs = {} if kwargs is None else kwargs

        args = tuple(a.key if isinstance(a, DistArray) else a for a in args)
        kwargs = {
            k: (v.key if isinstance(v, DistArray) else v)
            for k, v in kwargs.items()
        }

        targets = self.targets if targets is None else targets

        apply_nonce = nonce()
        apply_metadata = (apply_nonce, self.context_key)

        if not isinstance(func, types.BuiltinFunctionType):
            # break up the function
            func_code = func.__code__
            func_name = func.__name__
            func_defaults = func.__defaults__
            func_closure = func.__closure__

            func_data = (func_code, func_name, func_defaults, func_closure)

            msg = ('func_call', func_data, args, kwargs, apply_metadata,
                   autoproxyize)

        else:
            msg = ('builtin_call', func, args, kwargs, autoproxyize)

        self._send_msg(msg, targets=targets)
        return self._recv_msg(targets=targets)
Esempio n. 2
0
    def apply(self, func, args=None, kwargs=None, targets=None, autoproxyize=False):
        """
        Analogous to IPython.parallel.view.apply_sync

        Parameters
        ----------
        func : function
        args : tuple
            positional arguments to func
        kwargs : dict
            keyword arguments to func
        targets : sequence of integers
            engines func is to be run on.
        autoproxyize: bool, default False
            If True, implicitly return a Proxy object from the function.

        Returns
        -------
        list
            result from each engine.
        """
        # default arguments
        args = () if args is None else args
        kwargs = {} if kwargs is None else kwargs

        args = tuple(a.key if isinstance(a, DistArray) else a for a in args)
        kwargs = {k: (v.key if isinstance(v, DistArray) else v) for k, v in kwargs.items()}

        targets = self.targets if targets is None else targets

        apply_nonce = nonce()
        apply_metadata = (apply_nonce, self.context_key)

        if not isinstance(func, types.BuiltinFunctionType):
            # break up the function
            func_code = func.__code__
            func_name = func.__name__
            func_defaults = func.__defaults__
            func_closure = func.__closure__

            func_data = (func_code, func_name, func_defaults, func_closure)

            msg = ('func_call', func_data, args, kwargs, apply_metadata, autoproxyize)

        else:
            msg = ('builtin_call', func, args, kwargs, autoproxyize)

        self._send_msg(msg, targets=targets)
        return self._recv_msg(targets=targets)
Esempio n. 3
0
    def apply(self,
              func,
              args=None,
              kwargs=None,
              targets=None,
              autoproxyize=False):
        """
        Analogous to IPython.parallel.view.apply_sync

        Parameters
        ----------
        func : function
        args : tuple
            positional arguments to func
        kwargs : dict
            key word arguments to func
        targets : sequence of integers
            engines func is to be run on.
        autoproxyize: bool, default False
            If True, implicitly return a Proxy object from the function.

        Returns
        -------
            return a list of the results on the each engine.
        """
        def func_wrapper(func, apply_nonce, context_key, args, kwargs,
                         autoproxyize):
            """
            Function which calls the applied function after grabbing all the
            arguments on the engines that are passed in as names of the form
            `__distarray__<some uuid>`.
            """
            from importlib import import_module
            import types
            from distarray.metadata_utils import arg_kwarg_proxy_converter
            from distarray.localapi import LocalArray

            main = import_module('__main__')
            main.proxyize.set_state(apply_nonce)

            # Modify func to change the namespace it executes in.
            # but builtins don't have __code__, __globals__, etc.
            if not isinstance(func, types.BuiltinFunctionType):
                # get func's building  blocks first
                func_code = func.__code__
                func_name = func.__name__
                func_defaults = func.__defaults__
                func_closure = func.__closure__

                # build the func's new execution environment
                main.__dict__.update({'context_key': context_key})
                new_func_globals = main.__dict__
                # create the new func
                func = types.FunctionType(func_code, new_func_globals,
                                          func_name, func_defaults,
                                          func_closure)

            args, kwargs = arg_kwarg_proxy_converter(args, kwargs)
            result = func(*args, **kwargs)

            if autoproxyize and isinstance(result, LocalArray):
                return main.proxyize(result)
            else:
                return result

        # default arguments
        args = () if args is None else args
        kwargs = {} if kwargs is None else kwargs

        args = tuple(a.key if isinstance(a, DistArray) else a for a in args)
        kwargs = {
            k: (v.key if isinstance(v, DistArray) else v)
            for k, v in kwargs.items()
        }

        apply_nonce = nonce()
        wrapped_args = (func, apply_nonce, self.context_key, args, kwargs,
                        autoproxyize)

        targets = self.targets if targets is None else targets

        with self.view.temp_flags(targets=targets):
            return self.view.apply_sync(func_wrapper, *wrapped_args)
Esempio n. 4
0
    def apply(self, func, args=None, kwargs=None, targets=None, autoproxyize=False):
        """
        Analogous to IPython.parallel.view.apply_sync

        Parameters
        ----------
        func : function
        args : tuple
            positional arguments to func
        kwargs : dict
            key word arguments to func
        targets : sequence of integers
            engines func is to be run on.
        autoproxyize: bool, default False
            If True, implicitly return a Proxy object from the function.

        Returns
        -------
            return a list of the results on the each engine.
        """

        def func_wrapper(func, apply_nonce, context_key, args, kwargs, autoproxyize):
            """
            Function which calls the applied function after grabbing all the
            arguments on the engines that are passed in as names of the form
            `__distarray__<some uuid>`.
            """
            from importlib import import_module
            import types
            from distarray.metadata_utils import arg_kwarg_proxy_converter
            from distarray.localapi import LocalArray

            main = import_module('__main__')
            main.proxyize.set_state(apply_nonce)

            # Modify func to change the namespace it executes in.
            # but builtins don't have __code__, __globals__, etc.
            if not isinstance(func, types.BuiltinFunctionType):
                # get func's building  blocks first
                func_code = func.__code__
                func_name = func.__name__
                func_defaults = func.__defaults__
                func_closure = func.__closure__

                # build the func's new execution environment
                main.__dict__.update({'context_key': context_key})
                new_func_globals = main.__dict__
                # create the new func
                func = types.FunctionType(func_code, new_func_globals,
                                          func_name, func_defaults,
                                          func_closure)

            args, kwargs = arg_kwarg_proxy_converter(args, kwargs)
            result = func(*args, **kwargs)

            if autoproxyize and isinstance(result, LocalArray):
                return main.proxyize(result)
            else:
                return result

        # default arguments
        args = () if args is None else args
        kwargs = {} if kwargs is None else kwargs

        args = tuple(a.key if isinstance(a, DistArray) else a for a in args)
        kwargs = {k: (v.key if isinstance(v, DistArray) else v) for k, v in kwargs.items()}

        apply_nonce = nonce()
        wrapped_args = (func, apply_nonce, self.context_key, args, kwargs, autoproxyize)

        targets = self.targets if targets is None else targets

        with self.view.temp_flags(targets=targets):
            return self.view.apply_sync(func_wrapper, *wrapped_args)