Django-是否应该始终通过任务处理程序(例如Celery)发出外部API请求? [英] Django - Should external API requests always be made through a task handler (e.g. Celery)?

查看:30
本文介绍了Django-是否应该始终通过任务处理程序(例如Celery)发出外部API请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Django应用,在其中创建了一个自定义中间件.

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

它的工作方式如下:

  1. 中间件在每个请求中拦截一个令牌(标识用户),并使用该令牌向外部API发出请求.
  2. 外部API返回发出原始请求的用户所拥有的权限.
  3. 中间件完成,用户根据其权限返回数据

这是我的问题: 因为我的应用程序必须等待API请求返回才能处理请求,所以使用诸如celery之类的任务队列仍然有意义吗?等待响应时,它是否还不必阻塞线程?

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?

推荐答案

不,在这里使用Celery根本没有任何意义.那是完全可以进行处理的任务.一个很好的例子是发送确认电子邮件.发送到浏览器的响应不必等待电子邮件的发送,因为它不以任何方式依赖它.

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.

在您的情况下,响应明确地确实取决于API中的值.使用Celery无法获得任何好处,这会使整个过程变得比所需复杂得多.

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.

这篇关于Django-是否应该始终通过任务处理程序(例如Celery)发出外部API请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆