Search Options

Display Count
Sort
Preferred Language
Advanced Search

Results 1 - 10 of 31 for asynccontextmanager (0.11 seconds)

  1. docs_src/events/tutorial003_py310.py

    from contextlib import asynccontextmanager
    
    from fastapi import FastAPI
    
    
    def fake_answer_to_everything_ml_model(x: float):
        return x * 42
    
    
    ml_models = {}
    
    
    @asynccontextmanager
    async def lifespan(app: FastAPI):
        # Load the ML model
        ml_models["answer_to_everything"] = fake_answer_to_everything_ml_model
        yield
        # Clean up the ML models and release the resources
        ml_models.clear()
    
    
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Thu Feb 12 13:19:43 GMT 2026
    - 569 bytes
    - Click Count (0)
  2. tests/test_router_events.py

    
    def test_merged_mixed_state_lifespans() -> None:
        @asynccontextmanager
        async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
            yield
    
        @asynccontextmanager
        async def router_lifespan(app: FastAPI) -> AsyncGenerator[dict[str, bool], None]:
            yield {"router": True}
    
        @asynccontextmanager
        async def sub_router_lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Tue Feb 17 09:59:14 GMT 2026
    - 11.7K bytes
    - Click Count (0)
  3. docs_src/app_testing/tutorial004_py310.py

    from contextlib import asynccontextmanager
    
    from fastapi import FastAPI
    from fastapi.testclient import TestClient
    
    items = {}
    
    
    @asynccontextmanager
    async def lifespan(app: FastAPI):
        items["foo"] = {"name": "Fighters"}
        items["bar"] = {"name": "Tenders"}
        yield
        # clean up items
        items.clear()
    
    
    app = FastAPI(lifespan=lifespan)
    
    
    @app.get("/items/{item_id}")
    async def read_items(item_id: str):
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Thu Feb 12 13:19:43 GMT 2026
    - 1.2K bytes
    - Click Count (0)
  4. fastapi/concurrency.py

    from collections.abc import AsyncGenerator
    from contextlib import AbstractContextManager
    from contextlib import asynccontextmanager as asynccontextmanager
    from typing import TypeVar
    
    import anyio.to_thread
    from anyio import CapacityLimiter
    from starlette.concurrency import iterate_in_threadpool as iterate_in_threadpool  # noqa
    from starlette.concurrency import run_in_threadpool as run_in_threadpool  # noqa
    from starlette.concurrency import (  # noqa
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Wed Dec 17 21:25:59 GMT 2025
    - 1.5K bytes
    - Click Count (0)
  5. fastapi/dependencies/utils.py

        lenient_issubclass,
        sequence_types,
        serialize_sequence_value,
        value_is_sequence,
    )
    from fastapi.background import BackgroundTasks
    from fastapi.concurrency import (
        asynccontextmanager,
        contextmanager_in_threadpool,
    )
    from fastapi.dependencies.models import Dependant
    from fastapi.exceptions import DependencyScopeError
    from fastapi.logger import logger
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Sun Mar 15 11:44:39 GMT 2026
    - 38.7K bytes
    - Click Count (3)
  6. docs/en/docs/advanced/events.md

    And the part after the `yield` will be executed **after** the application has finished.
    
    ### Async Context Manager { #async-context-manager }
    
    If you check, the function is decorated with an `@asynccontextmanager`.
    
    That converts the function into something called an "**async context manager**".
    
    {* ../../docs_src/events/tutorial003_py310.py hl[1,13] *}
    
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Thu Mar 05 18:13:19 GMT 2026
    - 7.8K bytes
    - Click Count (0)
  7. fastapi/routing.py

        Coroutine,
        Generator,
        Iterator,
        Mapping,
        Sequence,
    )
    from contextlib import (
        AbstractAsyncContextManager,
        AbstractContextManager,
        AsyncExitStack,
        asynccontextmanager,
    )
    from enum import Enum, IntEnum
    from typing import (
        Annotated,
        Any,
        TypeVar,
        cast,
    )
    
    import anyio
    from annotated_doc import Doc
    from anyio.abc import ObjectReceiveStream
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Sun Mar 15 11:44:39 GMT 2026
    - 193K bytes
    - Click Count (0)
  8. docs/es/docs/tutorial/dependencies/dependencies-with-yield.md

    Cualquier función que sea válida para usar con:
    
    * [`@contextlib.contextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager) o
    * [`@contextlib.asynccontextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager)
    
    sería válida para usar como una dependencia en **FastAPI**.
    
    De hecho, FastAPI usa esos dos decoradores internamente.
    
    ///
    
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Thu Mar 19 18:15:55 GMT 2026
    - 13.3K bytes
    - Click Count (0)
  9. docs/en/docs/tutorial/dependencies/dependencies-with-yield.md

    Any function that is valid to use with:
    
    * [`@contextlib.contextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager) or
    * [`@contextlib.asynccontextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager)
    
    would be valid to use as a **FastAPI** dependency.
    
    In fact, FastAPI uses those two decorators internally.
    
    ///
    
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Thu Mar 05 18:13:19 GMT 2026
    - 12.6K bytes
    - Click Count (0)
  10. docs/zh-hant/docs/tutorial/dependencies/dependencies-with-yield.md

    ///
    
    /// note | 技術細節
    
    任何可用於下列裝飾器的函式:
    
    * [`@contextlib.contextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.contextmanager) 或
    * [`@contextlib.asynccontextmanager`](https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager)
    
    都可以作為 **FastAPI** 的相依。
    
    事實上,FastAPI 內部就是使用這兩個裝飾器。
    
    ///
    
    ## 使用 `yield` 的資料庫相依 { #a-database-dependency-with-yield }
    
    例如,你可以用它建立一個資料庫 session,並在完成後關閉。
    
    Created: Sun Apr 05 07:19:11 GMT 2026
    - Last Modified: Fri Mar 20 17:05:38 GMT 2026
    - 11.9K bytes
    - Click Count (0)
Back to Top