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.
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: