1from __future__ import annotations 2 3import warnings 4from typing import Awaitable, Callable, Optional 5 6from .run import worker_serve 7from ..config import Config 8from ..typing import ASGIFramework 9 10 11async def serve( 12 app: ASGIFramework, 13 config: Config, 14 *, 15 shutdown_trigger: Optional[Callable[..., Awaitable[None]]] = None, 16) -> None: 17 """Serve an ASGI framework app given the config. 18 19 This allows for a programmatic way to serve an ASGI framework, it 20 can be used via, 21 22 .. code-block:: python 23 24 asyncio.run(serve(app, config)) 25 26 It is assumed that the event-loop is configured before calling 27 this function, therefore configuration values that relate to loop 28 setup or process setup are ignored. 29 30 Arguments: 31 app: The ASGI application to serve. 32 config: A Hypercorn configuration object. 33 shutdown_trigger: This should return to trigger a graceful 34 shutdown. 35 """ 36 if config.debug: 37 warnings.warn("The config `debug` has no affect when using serve", Warning) 38 if config.workers != 1: 39 warnings.warn("The config `workers` has no affect when using serve", Warning) 40 41 await worker_serve(app, config, shutdown_trigger=shutdown_trigger) 42