我为什么要建立与异步/非阻塞框架的API? [英] Why should I build an API with an asynchronous/non-blocking framework?

查看:481
本文介绍了我为什么要建立与异步/非阻塞框架的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找进入播放框架作为一个可能的候选帮助我建立一个简单的API。但是,的Django的REST框架(DRF)也似乎是一个pretty强contenter。

I have been looking into the Play Framework as a possible candidate for helping me to build a simple API. However, the Django Rest Framework (DRF) also seems to be a pretty strong contenter.

据我所知道的,DRF不宣传自己,喜欢玩框架确实是一个异步的(或非阻塞)框架,但我感兴趣的是这是否甚至事宜。我一直在想的情况是通过山魈发送电子邮件到用户 - 我不希望我的API来陷入下来等待山魈API来告诉它的电子邮件是否被发送

As far as I can tell, the DRF does not advertise itself to be an asynchronous (or non-blocking) framework like the Play Framework does, but I am interested in whether or not this even matters. The situation that I keep thinking of is sending an email to a user via Mandrill -- I do not want my API to get bogged-down waiting for the Mandrill API to tell it whether or not the email was sent.

因此​​,我认为这个问题可以这样概括:有没有从客户的角度来看,一个好处,这将导致从我在DRF建立一个API与异步/非阻塞框架如播放,还是我失去了点?

Thus, I think the question can be summarized like this: is there a benefit from the client's perspective that will result from my building an API with an asynchronous/non-blocking framework like Play over the DRF, or am I missing the point?

推荐答案

我是一个Django的REST框架贡献者(和用户),所以我的观点是偏向了。

I'm a Django REST framework contributor (and user), so my perspective is biased towards that.

Django的REST框架是建立在Django的,这是用于网络应用程序的同步的框架。如果您已经使用类似Django的同步框架,有一个同步的API的不再是一个问题

Django REST framework is built on Django, which is a synchronous framework for web applications. If you're already using a synchronous framework like Django, having a synchronous API is less of an issue.

现在,仅仅因为它是同步的,这并不意味着只有一个请求都不能在一个时间进行处理。被处理的Django应用程序可以处理多个请求大多数Web服务器的,有些主题甚至做的somewhat异步多个线程的。通常,这实际上不是一个问题,因为您的Web服务器通常可以处理多个并发请求,即使他们中的一些阻塞。而当你有长,阻塞调用你通常不希望API内完成 - 你应该委托的背景工人一样的芹菜 Resque

Now, just because it is synchronous, that doesn't mean that only a single request can ever be handled at a time. Most web servers that are handling Django applications can handle multiple requests, some of theme even do it somewhat asynchronously across multiple threads. Usually this isn't actually an issue, as your web server can typically handle many concurrent requests, even if some of them are blocking. And when you have long, blocking calls you usually don't want that done within the API - you should be delegating that to background workers like Celery or Resque.

这不只是具体的Django,许多相同的原则也适用于像Rails和ASP.NET MVC其它同步框架。如果你长时间运行的请求,你一般应工作委派给其他进程,而不是阻碍了请求。这是通常使用的202响应code 对于这些情况

This isn't just specific to Django, many of the same principles apply to other synchronous frameworks like Rails and ASP.NET MVC. If you have long-running requests, you generally should be delegating work to other processes instead of holding up the request. It's common to use the 202 response code for these cases.

现在,这并不一定意味着异步框架是无用的。在运行时,如Node.js的,大多数的框架处理请求异步的。它没有意义在这些语言使用同步框架,因此,大多数图书馆都建是异步的。

Now, that doesn't necessarily mean that asynchronous frameworks are useless. In runtimes such as Node.js, most frameworks handle requests asynchronously. It doesn't make sense to use a synchronous framework in these languages, so most libraries are built to be asynchronous.

您选择非常有什么取决于你正在使用的工具。

What you choose very much depends on the tools that you are already using.

这篇关于我为什么要建立与异步/非阻塞框架的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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