コード例 #1
0
def get_client(ask=True):
    """Returns a Client object, from which you can construct a view.

    This may return None if no client is available, if the user cancels, etc.
    In this case, you might want to raise a ModuleError.
    """
    from engine_manager import EngineManager

    c = EngineManager.ensure_controller(connect_only=not ask)
    if c is not None and ask and not c.ids:
        EngineManager.start_engines(
                prompt="A module requested an IPython cluster, but no engines "
                       "are started. Do you want to start some?")
    if c is not None and c.ids:
        return c
    else:
        return None
コード例 #2
0
def parallel_map(function, *args, **kwargs):
    """Wrapper around IPython's map_sync() that defaults to map().

    This might use IPython's parallel map_sync(), or the standard map()
    function if IPython cannot be used.

    If the 'ask' keyword argument is true, the user will be prompted to start
    IPython engines, but the function will still default to map() if the user
    cancels.
    If the 'ipython' keyword argument is True, the function will return an
    additional boolean indicating whether this was computed through IPython
    (True) or with the default map() function (False).
    """
    say_ipython = kwargs.pop('ipython', False)
    ask = kwargs.pop('ask', False)
    if kwargs:
        raise TypeError("map() got unexpected keyword arguments")

    try:
        import IPython.parallel
    except ImportError:
        result, ipython = map(function, *args), False
    else:
        from engine_manager import EngineManager
        c = EngineManager.ensure_controller(connect_only=not ask)
        if c is not None and not c.ids:
            EngineManager.start_engines(
                    prompt="A module is performing a parallelizable "
                    "operation, however no IPython engines are running. Do "
                    "you want to start some?")

        if c is None or not c.ids:
            result, ipython = map(function, *args), False
        else:
            ldview = c.load_balanced_view()
            result, ipython = ldview.map_sync(function, *args), True

    if say_ipython:
        return result, ipython
    else:
        return result