Skip to main content

A small library to patch FastAPI to remove deadlock issues when using sync database sessions.

Project description

FastAPI Overflow

When using FastAPI with a synchronous SQLAlchemy session dependency, per the official example https://fastapi.tiangolo.com/tutorial/sql-databases/, high loads can lead to temporary deadlocks, timeouts and poor performance.

This is a known issue, for which a PR is open. This library exists to allow users to easily patch the issue and run their code in the mean time.

We provide 3 primary functions:

  • fastapi_overflow.patch which should be called before starting your app in order to patch FastAPI's thread handling
  • fastapi_overflow.run_in_threadpool which should be used instead of starlette.concurrency.run_in_threadpool or fastapi.concurrency.run_in_threadpool
  • fastapi_overflow.iterate_in_threadpool which should be used instead of starlette.concurrency.iterate_in_threadpool or fastapi.concurrency.iterate_in_threadpool

Usage

Use it like this:

from fastapi import FastAPI
from fastapi_overflow import patch

patch()  # run this before starting your app
app = FastAPI()

Changing the default thread limit

You can increase the number of threads from the default (40) using the fastapi_overflow.set_thread_limit function. With a very high number of threads you might also want to increase the reserve used to prevent deadlocks from its default of 1. You will need to add this to your startup events, or lifetime function:

@app.on_event("startup")
def on_startup():
    set_thread_limit(limit=..., anti_deadlock_reserve=2)
    ...  # any other setup here

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fastapi_overflow-0.1.3.tar.gz (7.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fastapi_overflow-0.1.3-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file fastapi_overflow-0.1.3.tar.gz.

File metadata

  • Download URL: fastapi_overflow-0.1.3.tar.gz
  • Upload date:
  • Size: 7.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for fastapi_overflow-0.1.3.tar.gz
Algorithm Hash digest
SHA256 d2cc686b3a6940a443241571a327e2c096a486f2ca2212c4f6b1543a11bd012c
MD5 30b906d9b985d92ac223711777a7b8c4
BLAKE2b-256 4648f7602493f64d10f460d19d2bc0f52f3cdd2e21722408c1330df7be969c86

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_overflow-0.1.3.tar.gz:

Publisher: ci.yml on olliemath/fastapi-overflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file fastapi_overflow-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for fastapi_overflow-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 c4ec856f401a978036e9809b8e0a5d53d810333f0b644a6ca501f48ddbaa3e94
MD5 d69ee8934cdddd6f5dd8034033237df0
BLAKE2b-256 2a85db09fa577d1d3385149f1effc78581311af2d00b7af662f316df0ca92d5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for fastapi_overflow-0.1.3-py3-none-any.whl:

Publisher: ci.yml on olliemath/fastapi-overflow

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page