# Copyright (c) 2020 CloudZero - ALL RIGHTS RESERVED - PROPRIETARY AND CONFIDENTIAL
# Unauthorized copying of this file and/or project, via any medium is strictly prohibited.
# Direct all questions to legal@cloudzero.com

import asyncio
import functools
from collections.abc import Callable, Iterable

from deprecated import deprecated


@deprecated(
    reason='Python 3.6 is deprecated for use within CloudZero, and support will be discontinued soon. Migrate to Python 3.8 now'
)
def async_map(function: Callable, arguments: Iterable[Iterable]) -> Iterable:
    """Asyncronously map the synchronous `function` over a list of
    `arguments` using asyncio."""

    loop = asyncio.new_event_loop()

    tasks = [loop.run_in_executor(None, functools.partial(function, *x)) for x in arguments]

    results = loop.run_until_complete(asyncio.gather(*tasks, loop=loop))  # type: ignore
    loop.close()
    return results
