def run_async_gateway(gateway, stop_task=None):
    """Run an async gateway."""
    try:
        gateway.tasks.loop.run_until_complete(gateway.start_persistence())
        gateway.tasks.loop.run_until_complete(gateway.start())
        gateway.tasks.loop.run_forever()
    except KeyboardInterrupt:
        gateway.tasks.loop.run_until_complete(gateway.stop())
        if stop_task:
            gateway.tasks.loop.run_until_complete(stop_task)
        gateway.tasks.loop.close()
