Integrations
Integration modules for popular Python frameworks and libraries.
FastAPI
Middleware and utilities for integrating cancellation with FastAPI applications.
hother.cancelable.integrations.fastapi
FastAPI integration for request-scoped cancelation.
RequestCancelationMiddleware
FastAPI middleware that provides request-scoped cancelation.
Source code in src/hother/cancelable/integrations/fastapi.py
CancelableWebSocket
WebSocket wrapper with cancelation support.
Source code in src/hother/cancelable/integrations/fastapi.py
get_request_token
get_request_token(request: Request) -> CancelationToken
Get cancelation token from request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI request |
required |
Returns:
| Type | Description |
|---|---|
CancelationToken
|
Cancelation token for this request |
Source code in src/hother/cancelable/integrations/fastapi.py
cancelable_dependency
async
cancelable_dependency(
request: Request, timeout: float | None = None
) -> Cancelable
FastAPI dependency that provides a cancelable for the request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
FastAPI request |
required |
timeout
|
float | None
|
Optional timeout override |
None
|
Returns:
| Type | Description |
|---|---|
Cancelable
|
Cancelable instance for this request |
Example
@app.get("/data") async def get_data( cancel: Cancelable = Depends(cancelable_dependency) ): async with cancel: return await fetch_data()
Source code in src/hother/cancelable/integrations/fastapi.py
with_cancelation
with_cancelation(
timeout: float | None = None,
raise_on_cancel: bool = True,
) -> Callable[[Callable[..., Any]], Callable[..., Any]]
Decorator for FastAPI endpoints with automatic cancelation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Optional timeout for the endpoint |
None
|
raise_on_cancel
|
bool
|
Whether to raise HTTPException on cancelation |
True
|
Returns:
| Type | Description |
|---|---|
Callable[[Callable[..., Any]], Callable[..., Any]]
|
Decorator function |
Example
@app.get("/slow") @with_cancelation(timeout=30.0) async def slow_endpoint(request: Request): # Cancelable is automatically injected cancelable = current_operation() await long_operation()
Source code in src/hother/cancelable/integrations/fastapi.py
cancelable_streaming_response
async
cancelable_streaming_response(
generator: AsyncIterator[Any],
cancelable: Cancelable,
media_type: str = "text/plain",
chunk_size: int | None = None,
) -> StreamingResponse
Create a streaming response with cancelation support.
Args:
generator: Async generator producing response chunks
cancelable: Cancelable instance
media_type: Response media type
chunk_size: Optional chunk size hint
Returns:
FastAPI StreamingResponse
Example:
@app.get("/stream")
async def stream_data(cancel: Cancelable = Depends(cancelable_dependency)):
async def generate():
for i in range(1000):
await anyio.sleep(0.1)
yield f"data: {i}
"
return await cancelable_streaming_response(
generate(),
cancel,
media_type="text/event-stream"
)