Fastapi: Limit concurrent operations on a certain endpoint

Created on 30 Jun 2020  路  3Comments  路  Source: tiangolo/fastapi

First check

  • [x] I added a very descriptive title to this issue.
  • [x] I used the GitHub search to find a similar issue and didn't find it.
  • [x] I searched the FastAPI documentation, with the integrated search.
  • [x] I already searched in Google "How to X in FastAPI" and didn't find any information.
  • [x] I already read and followed all the tutorial in the docs and didn't find an answer.
  • [x] I already checked if it is not related to FastAPI but to Pydantic.
  • [x] I already checked if it is not related to FastAPI but to Swagger UI.
  • [x] I already checked if it is not related to FastAPI but to ReDoc.
  • [x] After submitting this, I commit to one of:

    • Read open issues with questions until I find 2 issues where I can help someone and add a comment to help there.

    • I already hit the "watch" button in this repository to receive notifications and I commit to help at least 2 people that ask questions in the future.

    • Implement a Pull Request for a confirmed bug.

Description

Let's say I've got a POST endpoint to create certain object and persist it on a database, but there can't exist more than N objects. This validation is performed while processing the request, before persisting the object in database.

The problem with this is that, if another similar request to the same endpoint is performed, two objects would be created at the same time, when creating one would overflow this limit.

Has FastAPI some sort of tool to limit concurrency on a certain endpoint? Something similar to {threading,asyncio,multiprocessing}.Lock? (did not find anything on the documentation). If not, using one of the Python Lock objects be a possible workaround? However it might depend on how the API runs - if I'm not wrong, if runs in single process, non-async, would be threading; if runs in single process, async, would be asyncio; if runs with something like Gunicorn, which spawns multiple workers, I'm not sure if a native Lock would work.

question

All 3 comments

In this case i think a os level lock is better cause you need to prevent others proccess to execute too, an example is a exclusive file lock, but there鈥檙e others solutions too

I will try something like this: https://pypi.org/project/filelock/

There's not something integrated on FastAPI for that. It would probably be better to have it outside because you could also need to run your app in more than one server, or in multiple containers that don't share the same filesystem. You could probably use a Redis in the middle for that.

Anyway, thanks for the help here @rspadim! :clap: :bow:

Thanks for reporting back and closing the issue @David-Lor :+1:

Was this page helpful?
0 / 5 - 0 ratings