Home > Software design >  Django Handling Public API use (anonymouse users making calls to API)
Django Handling Public API use (anonymouse users making calls to API)

Time:08-02

I am making a simple website with as Django as backend. Ideally, you should be able to use it without creating an account and then all of your saved items ('notes') in there would be visible to anyone.

For now I have created a dummy user on Django, and every time an anonymous user makes an API call to add/delete/modify a notes, on Django's side it selects dummy user as a user.

It would work okey (I think) but one of Django's API can take a really long time to run (~1-2 minutes). Meaning that if multiple people are trying to make API calls while being anonymous, at some point the server will freeze until the long API finishes run.

Is there a way such case should be handed on the server side to prevent freezing of server ?

CodePudding user response:

As Sorin answered in comments, I would go into Celery way. Basically you can create model that collect the data when the last Celery task was ran - and, for example if it was not running since last 24 hours, and user visits the website, you run task again by async way.

You are not even forced to use AJAX calls for that, because sending task to Celery is fast enough to call it on get_context_data() or dispatch() methods. You could overload others, but these are the fastest and safest to override.

I hope the answer helps you!

  • Related