async def wait_tasks(tasks, flatten=True):
    '''Gather a list of asynchronous tasks and wait their completion.

    :param list tasks:
        A list of *asyncio* tasks wrapped in :func:`asyncio.ensure_future`.
    :param bool flatten:
        If ``True`` the returned results are flattened into one list if the
        tasks return iterable objects. The parameter does nothing if all the
        results are not iterable.
    :returns:
        The results of tasks as a list or as a flattened list
    '''
    rets = await asyncio.gather(*tasks)
    if flatten and all(map(lambda x: hasattr(x, '__iter__'), rets)):
        rets = list(itertools.chain(*rets))
    return rets
Gather a list of asynchronous tasks and wait their completion.

    :param list tasks:
        A list of *asyncio* tasks wrapped in :func:`asyncio.ensure_future`.
    :param bool flatten:
        If ``True`` the returned results are flattened into one list if the
        tasks return iterable objects. The parameter does nothing if all the
        results are not iterable.
    :returns:
        The results of tasks as a list or as a flattened list