Douglas Crockford 大神写的 JavaScript 异步控制库:RQ(上)

RQ

Douglas Crockford2015-10-06

翻译

RQ 是一个运行在服务端用于管理异步的小型JavaScript 库。

RQ is a small JavaScript library for managingasynchronicity in server applications.

源码在 https://github.com/douglascrockford/RQ。本页在 。采用公有领域。

The source is available at https://github.com/douglascrockford/RQ.This page is available at It is in the Public Domain.

异步 Asynchronicity

无论用户界面还是服务端,异步渐成为解决繁复问题的一种良好手段。异步函数会尽快地把控制权交到调用者手中。调用者通常会有片刻的等待,才会晓得通讯成功或失败。通讯过程可能会使用到回调函数或者延续(continuation)。

Asynchronicity is becoming the preferred method for solving a large classof problems, from user interfaces to servers. Asynchronous functionsreturn control to the caller almost immediately. Success or failure willbe communicated somehow in the future, but usually the caller will resumelong before that occurs. The communication will probably make use of somesort of callback function or continuation.

最简单来说,旧方式写法

At its simplest, it means that instead of writing

function oldway( . . . ) {. . .return result;}

将会改为现在的,

we now sometimes write

function newway(callback, . . . ) {. . .return callback(result);}

然而,这不是一句话两句话就能说完的。

But of course it is not that simple.

服务端所进行的工作流,往往与前端的有极大不同。一次请求可能会触发多个进程,当中完成了一个进程后就把交给下一个进程。也就是说,每个步骤可能依靠其他进程来处理,所以必须依靠回调来返回结果。等待的同时不会阻塞程序。原始的方法是,在前一个的回调函数中调用下一步。但通常这种程序写起来比较麻烦,难以阅读,也难以维护。

Servers offer workflows that are significantly different than those foundin user interfaces. An incoming message might require several processingsteps in which the result of each step is given to the next step. Sinceeach step may be concluded in a different processing turn, callbacks mustbe used. The program may not block while awaiting results. The naveapproach is start each step in the callback function of the previous step.This is a very awkward way to write, producing programs that are brittle,difficult to read, and difficult to maintain.

一个流程可以分为若干独立的步骤,意味着不但可以并行运行这些步骤,而且更重要的结果是会快很多。整体所消耗的时间仅仅是最满那次步骤之时间,非所有步骤所累加的时间——显然那会快很多。但是如果用简单的回调则不容易发挥并行的优点。原始的方法是,连续地执行每个步骤。但通常这种程序写起来比较麻烦,难以阅读,也难以维护。

A workflow might have several independent steps, which means that thosesteps could be taken in parallel, which could have significant performancebenefits. The elapsed time of the steps could be the slowest of all of thesteps, instead of the total of all of the steps, which could be a dramaticspeed up. But it is not obvious how to take advantage of parallelism withsimple callbacks. The nave approach is to perform each step serially.This is a very awkward way to write, producing programs that a brittle,difficult to read, difficult to maintain, and much too slow.

该并行模式是如此的困难以至于某些用户吐槽异步这玩意搞起来不大自然(或曰“反人类”)。但要搞清楚的是这不是异步本身的问题(异步其实是极好的),只不过我们没有适当的工具来异步罢了。尽管已经有了诸如 promise 等的工具来搞定那些问题,但 promise本身却不是为服务端而设计的。

This pattern is so problematic that some of its users have denouncedasynchronicity, declaring that it is unnatural and impossible to manage.But it turns out that the problem isn’t with asynchronicity. The problemis trying to do asynchronicity without proper tools. There are lots oftools available now, including promises. There are many good things thatcan be done with promises, but promises were not designed to help manageworkflows in servers.

于是,发明 RQ就有了合理的解释了。异步是好东西。我们不应该无视或拒绝它。我们应该尽量接受它,因为它是大势所趋。而 RQ就赋予了你这么一个简单的工具实现异步。

That is specifically what RQ was designed to do.Asynchronicity is our friend. We should not attempt to hide it or deny it.We must embrace asynchronicity because it is our destiny. RQgives you the simple tools you need to do that.

工作流 Workflow

RQ中的一个完整工作流可以分解成为若干的步骤(或称作任务或工作)。每个步骤视为函数。这些函数又可以称为“请求者(requestors)”,因为调用中很可能会发起请求。RQ的作用在于统筹这些请求者们,采用串行或并行的方式来处理它们。

With RQ, a workflow is broken into a set of steps or tasksor jobs. Each step is represented by a function. These functions arecalled requestors because calling one is likely to initiate arequest. RQ provides services that can collect somerequestors together and process them sequentially or in parallel.

举个例子,respond请求者首先会调用 getId,得到结果后把结果交给 getPreference请求者,然后又把得到的结果交割 getStuff 请求者。最后一个请求者 buildPage会把最后一个结果stuff 用于构建页面。

不如意的时候不要尽往悲伤里钻,想想有笑声的日子吧

Douglas Crockford 大神写的 JavaScript 异步控制库:RQ(上)

相关文章:

你感兴趣的文章:

标签云: