GitHub

uvicorn_server

API reference for uvicorn_server.

An HttpServer on uvicorn (extra pyfect-dev[server]; the counterpart of Effect's NodeHttpServer.layer)::

main = server.serve(app).pipe(layer.provide(uvicorn_server.layer(port=8080)))

The socket is bound when the server is made, so address is known (port 0: any free port). uvicorn runs on the building fiber's asyncio loop under run_main / run_async, else on a loop thread shared by such servers and never stopped (a fiber a request forks into a longer-lived set runs on it, and must still be able to finish once the server is gone); each request runs on its own fiber on that loop, through pyfect.http.asgi (streamed bodies, client aborts, WebSocket upgrades). A request that arrives before an app is served (while the app's layers are still being built) waits for it, as with NodeHttpServer. When the serving scope closes, uvicorn stops accepting and gives requests in flight graceful_shutdown_timeout; then their fibers are interrupted and streamed bodies end where they are (ASGI cannot abort a started response quietly, so it ends complete).

Re-exports

Classes

Functions

make

make(**options) -> Effect[HttpServerService, SocketServerError, ScopeTag]

An HttpServer on uvicorn, listening on host:port until the scope closes.

ParameterTypeDescription
optionsUnpack[Options] = {}

Returns Effect[HttpServerService, SocketServerError, ScopeTag]

def make(**options: Unpack[Options]) -> Effect[HttpServerService, SocketServerError, ScopeTag]:
    """An ``HttpServer`` on uvicorn, listening on ``host:port`` until the
    scope closes."""
    return make_listener(options, lambda dispatch: _uvicorn(dispatch, options))

layer_server

layer_server(**options) -> Layer[HttpServer, SocketServerError]
ParameterTypeDescription
optionsUnpack[Options] = {}

Returns Layer[HttpServer, SocketServerError]

def layer_server(**options: Unpack[Options]) -> Layer[HttpServer, SocketServerError]:
    return layer_effect(HttpServer, make(**options))

layer

layer(**options) -> Layer[HttpServer | Path | HttpPlatform | FileSystem | Generator, SocketServerError]

The server and the services its apps use (live_server.layer_services).

ParameterTypeDescription
optionsUnpack[Options] = {}

Returns Layer[HttpServer | Path | HttpPlatform | FileSystem | Generator, SocketServerError]

def layer(
    **options: Unpack[Options],
) -> Layer[HttpServer | Path | HttpPlatform | FileSystem | Generator, SocketServerError]:
    """The server and the services its apps use (``live_server.layer_services``)."""
    return merge(layer_server(**options), layer_services)

layer_test

layer_test(**options) -> Layer[HttpServer | Path | HttpPlatform | FileSystem | Generator | HttpClient, SocketServerError]

A server on a free loopback port, its services, and an HttpClient (httpx) sending to it (Effect's NodeHttpServer.layerTest).

ParameterTypeDescription
optionsUnpack[Options] = {}

Returns Layer[HttpServer | Path | HttpPlatform | FileSystem | Generator | HttpClient, SocketServerError]

def layer_test(
    **options: Unpack[Options],
) -> Layer[
    HttpServer | Path | HttpPlatform | FileSystem | Generator | HttpClient, SocketServerError
]:
    """A server on a free loopback port, its services, and an ``HttpClient``
    (httpx) sending to it (Effect's ``NodeHttpServer.layerTest``)."""
    server = layer(**{"host": "127.0.0.1", **options})
    return layer_test_client.pipe(provide_merge(httpx_layer)).pipe(provide_merge(server))