prefect.client.base

Functions

app_lifespan_context

app_lifespan_context(app: ASGIApp) -> AsyncGenerator[None, None]
A context manager that calls startup/shutdown hooks for the given application. Lifespan contexts are cached per application to avoid calling the lifespan hooks more than once if the context is entered in nested code. A no-op context will be returned if the context for the given application is already being managed. This manager is robust to concurrent access within the event loop. For example, if you have concurrent contexts for the same application, it is guaranteed that startup hooks will be called before their context starts and shutdown hooks will only be called after their context exits. A reference count is used to support nested use of clients without running lifespan hooks excessively. The first client context entered will create and enter a lifespan context. Each subsequent client will increment a reference count but will not create a new lifespan context. When each client context exits, the reference count is decremented. When the last client context exits, the lifespan will be closed. In simple nested cases, the first client context will be the one to exit the lifespan. However, if client contexts are entered concurrently they may not exit in a consistent order. If the first client context was responsible for closing the lifespan, it would have to wait until all other client contexts to exit to avoid firing shutdown hooks while the application is in use. Waiting for the other clients to exit can introduce deadlocks, so, instead, the first client will exit without closing the lifespan context and reference counts will be used to ensure the lifespan is closed once all of the clients are done.

determine_server_type

determine_server_type() -> ServerType
Determine the server type based on the current settings. Returns:
    • ServerType.EPHEMERAL if the ephemeral server is enabled
    • ServerType.SERVER if a API URL is configured and it is not a cloud URL
    • ServerType.CLOUD if an API URL is configured and it is a cloud URL
    • ServerType.UNCONFIGURED if no API URL is configured and ephemeral mode is not enabled

Classes

ASGIApp

PrefectResponse

A Prefect wrapper for the httpx.Response class. Provides more informative error messages. Methods:

from_httpx_response

from_httpx_response(cls: type[Self], response: httpx.Response) -> Response
Create a PrefectResponse from an httpx.Response. By changing the __class__ attribute of the Response, we change the method resolution order to look for methods defined in PrefectResponse, while leaving everything else about the original Response instance intact.

raise_for_status

raise_for_status(self) -> Response
Raise an exception if the response contains an HTTPStatusError. The PrefectHTTPStatusError contains useful additional information that is not contained in the HTTPStatusError.

PrefectHttpxAsyncClient

A Prefect wrapper for the async httpx client with support for retry-after headers for the provided status codes (typically 429, 502 and 503). Additionally, this client will always call raise_for_status on responses. For more details on rate limit headers, see: Configuring Cloudflare Rate Limiting Methods:

send

send(self, request: Request, *args: Any, **kwargs: Any) -> Response
Send a request with automatic retry behavior for the following status codes:
  • 403 Forbidden, if the request failed due to CSRF protection
  • 408 Request Timeout
  • 429 CloudFlare-style rate limiting
  • 502 Bad Gateway
  • 503 Service unavailable
  • Any additional status codes provided in PREFECT_CLIENT_RETRY_EXTRA_CODES

PrefectHttpxSyncClient

A Prefect wrapper for the async httpx client with support for retry-after headers for the provided status codes (typically 429, 502 and 503). Additionally, this client will always call raise_for_status on responses. For more details on rate limit headers, see: Configuring Cloudflare Rate Limiting Methods:

send

send(self, request: Request, *args: Any, **kwargs: Any) -> Response
Send a request with automatic retry behavior for the following status codes:
  • 403 Forbidden, if the request failed due to CSRF protection
  • 408 Request Timeout
  • 429 CloudFlare-style rate limiting
  • 502 Bad Gateway
  • 503 Service unavailable
  • Any additional status codes provided in PREFECT_CLIENT_RETRY_EXTRA_CODES

ServerType

Methods:

auto

auto() -> str
Exposes enum.auto() to avoid requiring a second import to use AutoEnum