| 248 | |
| 249 | # pylint: disable=W0613 |
| 250 | def run(self, dash_app: Dash, host: str, port: int, debug: bool, **kwargs: _t.Any): |
| 251 | import signal # pylint: disable=import-outside-toplevel |
| 252 | |
| 253 | # pylint: disable=import-outside-toplevel,import-error |
| 254 | from hypercorn.config import Config |
| 255 | from hypercorn.asyncio import serve |
| 256 | |
| 257 | # pylint: enable=import-error |
| 258 | |
| 259 | self.config = {"debug": debug, **kwargs} if debug else kwargs |
| 260 | # pylint: disable=protected-access |
| 261 | if dash_app._dev_tools.silence_routes_logging: |
| 262 | dictConfig( |
| 263 | { |
| 264 | "version": 1, |
| 265 | "loggers": { |
| 266 | "quart.app": { |
| 267 | "level": "ERROR", |
| 268 | }, |
| 269 | }, |
| 270 | } |
| 271 | ) |
| 272 | |
| 273 | # Check if we're running in a non-main thread (e.g., testing context) |
| 274 | is_main_thread = threading.current_thread() is threading.main_thread() |
| 275 | |
| 276 | config = Config() |
| 277 | config.bind = [f"{host}:{port}"] |
| 278 | config.use_reloader = False |
| 279 | if not is_main_thread: |
| 280 | config.accesslog = None |
| 281 | |
| 282 | loop = asyncio.new_event_loop() |
| 283 | asyncio.set_event_loop(loop) |
| 284 | |
| 285 | # Initialize shutdown event for WebSocket handlers |
| 286 | self._ws_shutdown_event = asyncio.Event() |
| 287 | |
| 288 | def signal_handler(): |
| 289 | """Handle shutdown signal by setting the WebSocket shutdown event.""" |
| 290 | if self._ws_shutdown_event is not None: |
| 291 | self._ws_shutdown_event.set() |
| 292 | |
| 293 | # Set up signal handlers in main thread |
| 294 | if is_main_thread: |
| 295 | for sig in (signal.SIGINT, signal.SIGTERM): |
| 296 | try: |
| 297 | loop.add_signal_handler(sig, signal_handler) |
| 298 | except (NotImplementedError, ValueError): |
| 299 | pass |
| 300 | |
| 301 | print(f" * Serving Quart app '{self.server.name}'") |
| 302 | print(f" * Debug mode: {debug}") |
| 303 | print( |
| 304 | " * Please use an ASGI server (e.g. Hypercorn) directly in production" |
| 305 | ) |
| 306 | print(f" * Running on http://{host}:{port} (CTRL + C to quit)") |
| 307 | |