How to ensure that a block of code can be executed only by one request at a time in FastAPI
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn’t find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google “How to X in FastAPI” and didn’t find any information.
- I already read and followed all the tutorial in the docs and didn’t find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
no code
Description
How to ensure that a block of code can be executed only by one request at a time in FAST API
I am implementing post and calling some external tool but it takes one request at a time.
Operating System
Linux
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
Python 3.8.5
Additional Context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
How to ensure that a block of code can be executed only by ...
Wrap your code in a mutex lock. In Python this can be done using multiprocessing.Lock ; for example: from multiprocessing import Lock mutex ......
Read more >Concurrency and async / await - FastAPI
You can only use await inside of functions created with async def . ... As the execution time is consumed mostly by waiting...
Read more >Concurrency with FastAPI - Medium
Here, GET request is made with the await keyword, telling Python that this is a point where it can suspend execution and do...
Read more >Speed Up Your Python Program With Concurrency
import concurrent.futures import requests import threading import time ... Lock to ensure that only one thread can access a block of code or...
Read more >FastAPI | PyCharm Documentation - JetBrains
Use coding assistance to develop an application · Use inspections to analyze code problems. You can use the inspection widget to briefly preview ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What might be the best solution is to make sure that nothing is async in your api route, and then run the server with only one worker. That way you ensure that requests wait for eachother.
uvicorn --workers 1
Read the documentation: https://fastapi.tiangolo.com/advanced/custom-request-and-route/