title: How to close a task in a loop
body: <p>I have a web server. I have a class for the visual interface. There is a class for processing incoming messages. I need to stop the execution of the task in a loop, or somehow break the connection. How can I stop the task {loop.create_task(master()} without closing the windows GUI ? For example, by pressing a button.</p>
  <pre><code>class ExampleApp(QtWidgets.QMainWindow, design.Ui_MainWindow):
      def __init__(self):
  
          super().__init__()
          self.setupUi(self)
  
  
  class ChargePoint(cp):
      @on(Action.BootNotification)
      def on_boot_notification(self, charge_point_vendor: str, charge_point_model: str, **kwargs):
          CP=DataBase.connect(DataBase.Get_ChargePoint())
          for row in CP:
              if charge_point_vendor == row[2] and charge_point_model == row[1]:
                  B=call_result.BootNotificationPayload(
                      current_time=datetime.utcnow().isoformat(),
                      interval=10,
                      status=RegistrationStatus.accepted
                  )
                  break
              else:
                  B=call_result.BootNotificationPayload(
                      current_time=datetime.utcnow().isoformat(),
                      interval=10,
                      status=RegistrationStatus.rejected
                  )
          return B
  app = QtWidgets.QApplication(sys.argv)
  loop = QEventLoop(app)
  window = ExampleApp()
  window.show()    
  async def on_connect(websocket, path):
      try:
          requested_protocols = websocket.request_headers[
              'Sec-WebSocket-Protocol']
      except KeyError:
          logging.error(
              &quot;Client hasn't requested any Subprotocol. Closing Connection&quot;
          )
          return await websocket.close()
      if websocket.subprotocol:
          logging.info(&quot;Protocols Matched: %s&quot;, websocket.subprotocol)
      else:
          logging.warning('Protocols Mismatched | Expected Subprotocols: %s,'
                          ' but client supports  %s | Closing connection',
                          websocket.available_subprotocols,
                          requested_protocols)
          return await websocket.close()
      charge_point_id = path.strip('/')
      cp = ChargePoint(charge_point_id, websocket)
  
      try:
          await cp.start()
      except websockets.exceptions.ConnectionClosedError:
          print(&quot;Разрыв соединения&quot;)
          print(charge_point_id)
      except websockets.exceptions.ConnectionClosed:
          print(&quot;Разрыв соединения&quot;)
          print(charge_point_id)
     
  async def main():
      server = await websockets.serve(
          on_connect,
          '127.0.0.1',
          9000,
          subprotocols=['ocpp1.6']
      )
      logging.info(&quot;Server Started listening to new connections...&quot;)
      await server.wait_closed()
  
  
  async def master():
      await main()
  
  
  loop.create_task(master())
  loop.run_forever()
  </code></pre>