博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript并发_限制JavaScript中的并发操作
阅读量:2522 次
发布时间:2019-05-11

本文共 3343 字,大约阅读时间需要 11 分钟。

javascript并发

Usually, the machine that executes our code has limited resources. Doing everything at once might not only hurt, but can also hang our process and make it stop responding altogether.

通常,执行我们代码的机器资源有限。 一次执行所有操作不仅可能会伤害您,而且可能使我们的流程挂起,并使它完全停止响应。

When we want to crawl 100 websites, we should crawl, for example, 5 at once, so that we don’t take up all the available bandwidth. As soon as one website is crawled, the next one is ready to go.

当我们要抓取100个网站时,我们应该一次抓取5个,这样我们就不会占用所有可用带宽。 爬网一个网站后,下一个网站就可以开始使用了。

Generally speaking, all “heavy” operations should be laid out in time. They should not be executed all-at-once, for better performance and to save resources.

一般而言,所有“繁重”的操作都应及时进行。 为了提高性能并节省资源,不应一次执行所有操作。

实作 (Implementation)

If you are familiar with my previous post about , then you are going to notice many similarities.

如果您熟悉我以前关于 ,那么您将注意到许多相似之处。

class Concurrently
{ private tasksQueue: (() => Promise
)[] = []; private tasksActiveCount: number = 0; private tasksLimit: number; public constructor(tasksLimit: number) { if (tasksLimit < 0) { throw new Error('Limit cant be lower than 0.'); } this.tasksLimit = tasksLimit; } private registerTask(handler) { this.tasksQueue = [...this.tasksQueue, handler]; this.executeTasks(); } private executeTasks() { while (this.tasksQueue.length && this.tasksActiveCount < this.tasksLimit) { const task = this.tasksQueue[0]; this.tasksQueue = this.tasksQueue.slice(1); this.tasksActiveCount += 1; task() .then((result) => { this.tasksActiveCount -= 1; this.executeTasks(); return result; }) .catch((err) => { this.tasksActiveCount -= 1; this.executeTasks(); throw err; }); } } public task(handler: () => Promise
): Promise
{ return new Promise((resolve, reject) => this.registerTask(() => handler() .then(resolve) .catch(reject), ), ); }}export default Concurrently;

We register a given task by adding it to our tasksQueue and then we call executeTasks.

我们通过将给定任务添加到taskQueue中来注册它,然后调用executeTasks

Now we execute as many tasks as our limit allows us — one by one. Each time adding 1 to our counter called tasksActiveCount.

现在,我们可以按限制执行尽可能多的任务,一个接一个。 每次将1加到我们的称为taskActiveCount的计数器中。

When the executed task finishes, we remove 1 from tasksActiveCount and again call executeTasks.

当执行的任务完成时,我们从taskActiveCount中删除1,然后再次调用executeTasks

Below we can see an example of how it works.

在下面,我们可以看到一个有关其工作方式的示例。

The limit is set to 3. The first two tasks are taking very long to process. We can see the third “slot” getting opened from time to time, allowing the next task in the queue to be executed.

限制设置为3。前两个任务的处理时间很长。 我们可以看到不时打开了第三个“插槽”,从而可以执行队列中的下一个任务。

Always there are three, no more, no less.

总是有三个,不多不少。

You can see the code in the .

您可以在查看代码。

Thank you very much for reading! Can you think of any other way of achieving the same effect? Share them down below.

非常感谢您的阅读! 您能想到其他实现相同效果的方法吗? 在下方分享。

If you have any questions or comments feel free to put them in the comment section below or send me a .

如果您有任何问题或评论,请随时将其放在下面的评论部分或给我发送 。

Check out my !

看看我的 !

!

Originally published at on August 28, 2018.

最初于年8月28日发布在上。

翻译自:

javascript并发

转载地址:http://jdwzd.baihongyu.com/

你可能感兴趣的文章
session概述
查看>>
MATLAB 单变量函数一阶及N阶求导
查看>>
如何在网页端启动WinForm 程序
查看>>
[转载] Java并发编程:Lock
查看>>
MySQL之索引
查看>>
JAVA设计模式之单例模式
查看>>
优秀博客
查看>>
词法分析程序
查看>>
Java反射
查看>>
[ACM_模拟][ACM_数学] LA 2995 Image Is Everything [由6个视图计算立方体最大体积]
查看>>
1040 有几个PAT
查看>>
BZOJ 1412 [ZJOI2009]狼和羊的故事 | 网络流
查看>>
原型模式
查看>>
Hadoop RPC源码阅读-交互协议
查看>>
WASAPI、DirectSound/DS、WaveOut、Kernel Streaming/KS
查看>>
Perl按行分割文件
查看>>
根据现有表操作基于active record的model
查看>>
NotMapped属性特性
查看>>
Count and Say
查看>>
GridView数据导入Excel/Excel数据读入GridView
查看>>