new·The score now tells you which way it movedA brain's exam only ever grows: its own material writes questions, and so does every question a real caller asked and did not get answered. The score is a percentage over that growing set, so a brain that learned more could post a smaller number — and this week three did. One of them answered two MORE questions than the week before and showed eighteen points less. Printed as a single percentage, that reads as decline to a reader and as punishment to anyone who contributes material.all news →
mozg.beta
Sign in

FastAPI · all subjects

async/await

9 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Use async def for path operations with await-able libraries

When using third-party libraries that require `await`, declare path operation functions with `async def`. For example: `@app.get('/') async def read_results(): results = await some_library(); return results`. You can only use `await` inside functions created with `async def`.

Use normal def for path operations without async support

When using libraries that communicate with external systems (databases, APIs, file systems) but do not support `await`, declare path operation functions with normal `def`. Example: `@app.get('/') def results(): results = some_library(); return results`. Most database libraries currently do not have async support.

Use async def even without await for applications with no external waits

If your application does not need to communicate with anything else or wait for external responses, use `async def` even if you don't need to use `await` inside.

Default to normal def if unsure about async

If you are uncertain whether to use `def` or `async def`, use normal `def`.

Mix def and async def in path operations

You can mix `def` and `async def` in your path operation functions as much as needed, defining each one using the best option. FastAPI will handle both correctly and still work asynchronously and very fast.

Async syntax with await keyword

Use `await` to wait for an asynchronous operation to complete before continuing. Syntax: `burgers = await get_burgers(2)`. The `await` keyword tells Python to pause execution at that point and allow other work to proceed, then resume when the awaited operation finishes.

async def declares asynchronous functions

Functions declared with `async def` support `await` expressions inside them and can be paused during execution. Example: `async def get_burgers(number: int): return burgers`. Functions with `async def` can only be called from within other `async def` functions using `await`.

Cannot call async def function without await

Calling an `async def` function without `await` returns a coroutine object without executing the function. This will not work: `burgers = get_burgers(2)` if `get_burgers` is defined with `async def`. You must use: `burgers = await get_burgers(2)`.

Async path operations in FastAPI

Path operation functions can use `async def` instead of regular `def`. Example: ```Python from fastapi import FastAPI app = FastAPI() @app.get("/") async def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") async def read_item(item_id: int, q: str | None = None): return {"item_id": item_id, "q": q} ```

Give your agent this brain