import asyncio


def synchronize_generator(async_generator, *args, **kwargs):
    """
    Returns a synchronous generator from an asynchronous generator
    """

    ag = async_generator(*args, **kwargs)

    async def consume_generator(stop_signal):
        r = await ag.asend(stop_signal)
        return r

    loop = asyncio.new_event_loop()
    try:
        stop_signal = None
        while not stop_signal:
            val = loop.run_until_complete(consume_generator(stop_signal))
            stop_signal = yield val
        if stop_signal:
            val = loop.run_until_complete(consume_generator(stop_signal))
            loop.close()
            loop = None
            yield val
    except StopAsyncIteration:
        loop.close()
        loop = None
    assert loop is None