-
-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: use async by default, with occasional def threading #1015
Conversation
for more information, see https://pre-commit.ci
Great Documentation ! FastAPI sometimes suffer with thread management specially when you are using run in threadpool ! I believe it totally depends upon usecase as you have mentioned , May be One more thing to add , if you are not dependent on external services and your API is doing heavy lifting and taking considerable time , I will recommend do not work on the managing the threads may be revisit the architecture you have , Optimize your endpoint. API is never supposed to do heavy lifting , may be assign different script for workers and let them do the heavy lifting , API is supposed to return fast , it will either fetch the data or submit the request and comeback , this practice might make architecture simple ! + when you have multiple threads open by single request , it might not scale when there are multiple users hitting the same endpoint , I like this part of fastapi that it can automatically distribute the request based on the load , it is quite efficient |
Great feedback, thanks @kshitijrajsharma! |
* build: add asgiref async_to_sync, plus pytest-asyncio * build: install script login linger after docker install * docs: update instructions to automount s3fs * fix: add user role to user schema, remove appuser logic * refactor: async initial load read_xlsforms * refactor: update refs to form --> category in project creation * refactor: remove redundant projects.utils (replaced) * refactor: rename add_features --> upload_custom_extract * fix: update all to async, except BackgroundTasks defs * fix: update long running central operations to run_in_threadpool * test: update tests to be async + threadpool * fix: update expensive generate_task_files to use ThreadPoolExecutor * fix: use threadpool for generate_task_files * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor: move logic for task schemas into models * refactor: remove final ref to convert_app_tasks * refactor: fix loading project summaries without db_obj * fix: add outline_centroid to task_schemas * refactor: add task details to project schema output --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Async
async def
) function with await is more scalable than using synchronous codedef
, so this is always the preferred default approach.Workers & Thread Blocking
Using Synchronous Code
It is of course possible to use synchronous code, but if necessary, be sure to run this in another thread.
Options
1) Using sync code within an
async def
functiondef
. FastAPI will handle this automatically and ensure it runs in a separate thread.run_in_threadpool
:2) Running multiple standard
def
from within anasync def
functiondef
functions in parallel.Note that in the above example, we cannot pass the db object from the parent function into the functions spawned in threads. A single database connection should not be written to by multiple processes at the same time, as you may get data inconsistencies. To solve this we generate a new db connection within the pool for each separate task we run in a thread.
3) Running an
async def
within a syncdef
async def
logic within a syncdef
. This is not possible normally.def
equivalent of theasync def
code, we can use the packageasgiref
:4) Efficiency running batch async tasks
asyncio.gather
to much more efficiently collect and return the async data (e.g. async web requests, or async file requests, or async db requests):Note