def KillEmulator()

in tools/android/emulator/emulated_device.py [0:0]


  def KillEmulator(self, politely=False, kill_over_telnet=True):
    """Stops the emulator.

    Args:
      politely: [optional] if true we do our best to ensure the emulator exits
        cleanly.
      kill_over_telnet: [optional] if true sends kill command over telnet
        otherwise just kills the emu process using os.kill
    """
    clean_death = True
    if politely and self._vm_running:
      idle = IdleStatus(device=self)
      # Wait for system being idle.
      for _ in range(40):
        time.sleep(6)
        load = idle.RecentMaxLoad(15)
        if load < 0.1:
          logging.info('Emulator is idle now.')
          break

      self.ExecOnDevice(['stop'])

      # stop any other processes that are not protected.
      running_svcs = [k for k, v in self._QueryServices().items()
                      if k not in SHUTDOWN_PROTECTED_SERVICES
                      and v != 'stopped']
      for svc in running_svcs:
        self.ExecOnDevice(['stop', svc])

      self._CheckLeftProcess()
      # Umount /data/media first. Otherwise umount of /data will fail.
      clean_death = self._CleanUmount('/data/media') and clean_death
      if self.GetApiVersion() >= 28:
        netns_router = '/data/vendor/var/run/netns/router'
      else:
        netns_router = '/data/var/run/netns/router'
      clean_death = self._CleanUmount(netns_router) and clean_death
      clean_death = self._CleanUmount('/data/var/run/netns') and clean_death
      clean_death = self._CleanUmount('/data') and clean_death
      clean_death = self._CleanUmount('/cache') and clean_death

    if kill_over_telnet:
      telnet = self._ConnectToEmulatorConsole()
      telnet.write('kill\n')
      telnet.read_all()
      self._running = False
    else:
      self._StopAllProcesses()
      self._running = False

    if politely and not clean_death:
      self._TransientDeath('Could not cleanly shutdown emulator', False)