Kedar Kulkarni
Kedar Kulkarni

Reputation: 95

Is there a way to block django views from serving multiple requests concurrently?

I have a django app, where I am using one of the views to fetch data from local filesystem and parse it and add it to my database. Now the thing is, I want to restrict this view from serving multiple requests concurrently, I want them to be served sequentially instead. Or just block the new request when one request is already being served. Is there a way to achieve it?

Upvotes: 1

Views: 325

Answers (2)

LhasaDad
LhasaDad

Reputation: 2133

here is a link to python functions and module that support inter-thread locking:

https://docs.python.org/3/library/asyncio-sync.html

There are some simple examples on the page.

Upvotes: 1

Daniel Roseman
Daniel Roseman

Reputation: 599470

You need some kind of mutex. Since your operations involve the filesystem already, perhaps you could use a file as a mutex. For instance, at the start of the operation, check if a specific file exists in a specific place; if it does, return an error, but if not, create it and proceed, deleting it at the end of the operation (making sure to also delete it in the case of any error).

Upvotes: 2

Related Questions