Metadata-Version: 2.4
Name: fastapi-overflow
Version: 0.1.1
Summary: A small library to patch FastAPI to remove deadlock issues when using sync database sessions.
Author-email: Oliver Margetts <oliver.margetts@gmail.com>
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-Expression: MIT
Classifier: Intended Audience :: Information Technology
Classifier: Intended Audience :: System Administrators
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python
Classifier: Topic :: Internet
Classifier: Topic :: Software Development :: Libraries :: Application Frameworks
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development
Classifier: Typing :: Typed
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Web Environment
Classifier: Framework :: AsyncIO
Classifier: Framework :: FastAPI
Classifier: Framework :: Pydantic
Classifier: Framework :: Pydantic :: 2
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Internet :: WWW/HTTP
License-File: LICENSE
Requires-Dist: fastapi >= 0.128.0
Requires-Dist: httpx ; extra == "dev"
Requires-Dist: mypy ; extra == "dev"
Requires-Dist: pytest ; extra == "dev"
Requires-Dist: ruff ; extra == "dev"
Project-URL: Homepage, https://github.com/olliemath/fastapi-overflow
Provides-Extra: dev

# 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:

```python
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:

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

