import socket
import collections


def get_engines_by_host(client):
    """ returns the engine ids by host

    Parameters
    ----------
    client : IPython.parallel.Client instance

    Returns
    -------
    dict
        a dict with hostnames as keys, and a list
        of engine ids

    """

    results = {i: client[i].apply_sync(socket.gethostname) for i in client.ids}

    engines_by_host = collections.defaultdict(list)
    for engine_id, host in results.items():
        engines_by_host[host].append(engine_id)
    return engines_by_host