import asyncio


def run(coro, forever=False):
    """Convenience function that makes an event loop and runs the async
    function within it.

    Args:
        forever: If True then run the event loop forever, otherwise
            return on completion of the coro
    """
    loop = asyncio.get_event_loop()
    t = None
    try:
        if forever:
            t = asyncio.ensure_future(coro, loop=loop)
            loop.run_forever()
        else:
            return loop.run_until_complete(coro)
    finally:
        if t:
            t.cancel()
        loop.stop()
        loop.close()