code: def stop(self):
          
  
          if not self.loop:
              return
  
          if self.inside_loop():
              raise InternalError("BackgroundEventLoop.stop() called from inside event loop; "
                                  "would have deadlocked.")
  
          try:
              self.run_coroutine(self._stop_internal())
              self.thread.join()
          except:
              self._logger.exception("Error stopping BackgroundEventLoop")
              raise
          finally:
              self.thread = None
              self.loop = None
              self.tasks = set()
docs: Synchronously stop the background loop from outside.
  
          This method will block until the background loop is completely stopped
          so it cannot be called from inside the loop itself.
  
          This method is safe to call multiple times.  If the loop is not
          currently running it will return without doing anything.