Debian Codegraph @

debian/vendor/hypercorn/asyncio/__init__.py

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