def run(self) -> None: """ Start the event loop and run forever. """ log.debug("Running Websocket server") self.loop = asyncio.new_event_loop() asyncio.set_event_loop(self.loop) try: server_start = serve(self.handler, '127.0.0.1', WEBSOCKET_PORT, close_timeout=1) self.server = self.loop.run_until_complete(server_start) self.server_is_started.set() self.loop.run_forever() except OSError: # The code above may throw an OSError # if the socket cannot be bound log.exception("Server could not be started") finally: log.debug("loop stopped") log.debug("Pending tasks at close: %r", asyncio.all_tasks(self.loop)) self.loop.close() log.debug("loop closed") self.loop_is_closed.set()
async def _websocket_server(stop: "Future[Any]", host: str, port: int, close_timeout_sec: float) -> None: """Websocket server.""" log.info("Completed starting websocket server") # ok, it's a bit of a lie async with serve(_handle_connection, host=host, port=port, close_timeout=close_timeout_sec): await stop await _handle_shutdown()