fastapi-dependency
When you use FastAPI, you might be tempted to create sync (def) dependencies on which you actually don't perform thread blocking operations.
The thing is that FastAPI will always run your sync dependencies in a thread pool, which is not necessary.
The goal of this package is to make explicit if you want to run a dependency in a thread pool.
Installation
The package is available on PyPI:
pip install fastapi-dependency
Usage
This package is really small and contains simple functions:
Depends
Signature:
Depends(dependency: Callable[..., Any] | None = None, *, use_cache: bool = True, use_thread_pool: bool | None = None)
This function is a drop-in replacement for fastapi.Depends and it has the same signature.
The only difference is that it has an extra parameter: use_thread_pool.
If you want to run a dependency in a thread pool, you can set use_thread_pool to True.
from fastapi import FastAPI
from fastapi_dependency import Depends
app = FastAPI()
def dependency():
return "Hello World!"
@app.get("/")
def index(message: str = Depends(dependency, use_thread_pool=True)):
return {"message": message}
If you don't set use_thread_pool on sync dependencies, it will raise a RuntimeError.
ThreadDepends
Signature:
ThreadDepends(dependency: Callable[..., Any] | None = None, *, use_cache: bool = True)
This function is a drop-in replacement for fastapi.Depends and it has the same signature.
The only difference is that it will always run the dependency in a thread pool.
from fastapi import FastAPI
from fastapi_dependency import ThreadDepends
app = FastAPI()
def dependency():
return "Hello World!"
@app.get("/")
def index(message: str = ThreadDepends(dependency)):
return {"message": message}
ThreadlessDepends
Signature:
ThreadlessDepends(dependency: Callable[..., Any] | None = None, *, use_cache: bool = True)
This function is a drop-in replacement for fastapi.Depends and it has the same signature.
The only difference is that it will never run the dependency in a thread pool.
from fastapi import FastAPI
from fastapi_dependency import ThreadlessDepends
app = FastAPI()
def dependency():
return "Hello World!"
@app.get("/")
def index(message: str = ThreadlessDepends(dependency)):
return {"message": message}
Security
The analogous functions for fastapi.Security are:
SecurityThreadSecurityThreadlessSecurity
License
This project is licensed under the terms of the MIT license.
Metadata
Release files for fastapi-dependency 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fastapi_dependency-0.1.0.tar.gz | 6.5 kB | Without provenance | Details for fastapi_dependency-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fastapi_dependency-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for fastapi_dependency-0.1.0-py3-none-any.whl |
Total release size: 10.8 kB
Release files / fastapi_dependency-0.1.0.tar.gz
| Download URL | fastapi_dependency-0.1.0.tar.gz |
|---|---|
| Size | 6.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
97e001f9dd12e8c64939dd237689e7b32da36faf9fef400147e3462e33ec8cff
|
|
BLAKE2b-256 checksum How to use checksums |
1539f1109599a8acab58622734a51762446c84a42a96831bcccc8d63b94e6e8f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.3
|
Release files / fastapi_dependency-0.1.0-py3-none-any.whl
| Download URL | fastapi_dependency-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a954f4f57b2b222591c66f6ee894f583fd898e8d370630daf0647b8c88635b67
|
|
BLAKE2b-256 checksum How to use checksums |
a6c9634a1a662db56880a9dc7d69b4e24fe1137653e449aaf49de2a02e8d8850
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
python-httpx/0.23.3
|
Security for fastapi-dependency 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.