question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to ensure that a block of code can be executed only by one request at a time in FastAPI

See original GitHub issue

First 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:open
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
MatthijsKokcommented, Jan 21, 2022

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

0reactions
ycdcommented, Jan 21, 2022
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found