Skip to content

Commit 093c3e1

Browse files
committed
vault backup: 2025-01-02 11:41:59
1 parent ad70079 commit 093c3e1

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

content/programming/languages/python/fastapi/globals-in-fastapi.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ __engine: AsyncEngine = None
9898

9999

100100
async def get_engine() -> AsyncGenerator[AsyncEngine]:
101-
yield engine
101+
yield __engine
102102

103103

104104
@asynccontextmanager
@@ -173,21 +173,21 @@ The downside of this approach is that you can't use the database outside of Fast
173173
> import asyncio
174174
>
175175
> async def periodic_task(shutdown_event: asyncio.Event) -> None:
176-
> timeout = 100
177-
> while not shutdown_event.is_set():
178-
> try:
179-
> await asyncio.wait_for(shutdown_event.wait(), timeout=timeout)
180-
> break
181-
> except TimeoutError:
182-
> ... # your actual periodic task
176+
> timeout = 100
177+
> while not shutdown_event.is_set():
178+
> try:
179+
> await asyncio.wait_for(shutdown_event.wait(), timeout=timeout)
180+
> break
181+
> except TimeoutError:
182+
> ... # your actual periodic task
183183
>
184184
>
185185
> async def lifespan() -> AsyncGenerator[AppState]:
186-
> shutdown_event = asyncio.Event()
187-
> async with asyncio.TaskGroup() as tg:
188-
> tg.create_task(my_periodic_task(shutdown_event))
189-
> yield
190-
> shutdown_event.set()
186+
> shutdown_event = asyncio.Event()
187+
> async with asyncio.TaskGroup() as tg:
188+
> tg.create_task(my_periodic_task(shutdown_event))
189+
> yield
190+
> shutdown_event.set()
191191
> ```
192192
>
193193
> The upside of this design being that your TaskGroup will now clean itself up when the lifespan exits. You can additionally pass an object from the lifespan into the defined `periodic_task` (like a database engine), meaning we keep the lifespan philosophy intact.

0 commit comments

Comments
 (0)