Before jumping into Celery. Let's start with the most straightforward tool to help us understand background tasks. FastAPI already has a BackgroundTasks class that can help us implement simple background tasks. Let's create a virtual environment to isolate our project requirements. python -m venv env Now, all we need is FastAPI and a web server e.g. Uvicorn or Hypercorn. Before installing these le
はじめに FastAPIを始めてみたところ、async/await構文があり、Pythonにも「async/await構文があるんだなー」と初めてその存在を知った。 しかし、FastAPIのサンプルコードやネットで公開されているコードを見ると、async defとdefをどのように使い分けているのかよくわからず、結局、「どっちを使えば良いんじゃろう?」という気持ちになったので、async/await、同期 / 非同期(並行処理)を調べつつ、結論を導いてみることにした。 いきなり結論 Path operation 関数の場合、async defではなくdefで、基本、実装する。 defだけでも、外部スレッドプールで非同期処理されるようにフレームワークとして実装されているとのこと async defを使った方が良いのは以下の2ケース async/awaitをサポートしているライブラリを利用した
背景 FastAPIでは以下のようにデコレータ関数を使うことでHTTPサーバのpathを設定することができ、これをPath Operationと呼びます。 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): return {"item_id": item_id, "q": q} このPath Operationでdefを使うべきかasync defを使うべきかの方針を説明します。 環境 Python 3.9.11 FastAPI 0.74.1 方針 結論から言うと以下の方針で
FastAPI is a popular topic nowadays and I have decided to share my setup for an async web-server using this framework. Here is a short description of python packages used in the article (just to make a whole picture to save your time): Poetry (https://python-poetry.org) — is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and
FastAPI with Async SQLAlchemy, SQLModel, and Alembic This tutorial looks at how to work with SQLAlchemy asynchronously with SQLModel and FastAPI. We'll also configure Alembic for handling database migrations. This tutorial assumes you have experience working with FastAPI and Postgres using Docker. Need help getting up to speed with FastAPI, Postgres, and Docker? Start with the following resources:
リリース、障害情報などのサービスのお知らせ
最新の人気エントリーの配信
処理を実行中です
j次のブックマーク
k前のブックマーク
lあとで読む
eコメント一覧を開く
oページを開く