<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 async def _clean_shutdown(self):
 """Cleanly shutdown the emulation loop."""


        # Cleanly stop any other outstanding tasks not associated with tiles
        remaining_tasks = []
        for task in self._tasks.get(None, []):
            self._logger.debug("Cancelling task at shutdown %s", task)
            task.cancel()

            remaining_tasks.append(task)

        asyncio.gather(*remaining_tasks, return_exceptions=True)

        if len(remaining_tasks) > 0:
            del self._tasks[None]

        # Shutdown tasks associated with each tile
        remaining_tasks = []

        for address in sorted(self._tasks, reverse=True):
            if address is None:
                continue

            self._logger.debug("Shutting down tasks for tile at %d", address)
            for task in self._tasks.get(address, []):
                task.cancel()
                remaining_tasks.append(task)

        asyncio.gather(*remaining_tasks, return_exceptions=True)

        await self._rpc_queue.stop()

        self._loop.stop()