andrroy
andrroy

Reputation: 138

Django - Should external API requests always be made through a task handler (e.g. Celery)?

I have a Django app where I have created a custom middleware.

It works as follows:

  1. The middleware intercepts a token (which identifies the users) within each request, and makes a request to an external API with that token.
  2. The external API returns what permissions the user making the original request has.
  3. The middleware completes, and the user gets data returned based on its permissions

This is my question: Because my app has to wait for the API request to return before it can process the request, does it still make sense to use a task queue such as celery? Wouldn't it still have to block the thread while I waiting for the response?

Upvotes: 5

Views: 734

Answers (2)

auvipy
auvipy

Reputation: 1208

this is not mandatory in your described case, unless you need to handle thousands of this requests type of requests which need to be queued.

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599856

No, using Celery here wouldn't make any sense at all. That's for tasks that can be purely out-of-process. A good example is sending a confirmation email; the response sent to the browser doesn't have to wait for the email to be sent, because it doesn't depend on it in any way.

In your case, the response explicitly does depend on the value from the API. There would be nothing to be gained from using Celery, and it would make the whole process much more complex than it needs to be.

Upvotes: 8

Related Questions