1.BLOCKING THE EVENT LOOP
  Node and JavaScript runtimes in general are single-threaded event loops. On each loop, the runtime
processes the next event in queue by calling the associated callback function. When that event
is done, the event loop fetches and processes the next event; this pattern continues until the queue is
empty. If one of these callbacks takes a long time, the event loop won’t process pending events in the
meantime. This can lead to a slow application or service.
  Using memory- or processor-intensive functions when handling events can lead to the event loop
becoming slow and the events becoming queued up and unserved or even blocked.
Here is an example of blocking the event loop:

process.nextTick(function nextTick1() {
  var a = 0;
  while(true) {
    a ++;
  }
});
process.nextTick(function nextTick2() {
  console.log("next tick");
});
setTimeout(function timeout() {
  console.log("timeout");
}, 1000);

In this case, the nextTick2 and timeout functions will never have the chance to run no matter how
long you wait because the event loop is blocked by an infinite cycle inside the first nextTick function.
Even the scheduled timeout function that was supposed to run after one second does not run.
When using setTimeout, the callback function goes into a scheduling queue, which, in this
case, doesn’t even get to be dispatched. This is an extreme case, but you can see how running a
processor-intensive task may block or slow down your event loop.

2.ESCAPING THE EVENT LOOP
  By using process.nextTick, you can now defer the execution of a non-crucial task to the next tick,
freeing the event loop to execute other pending events.
As an example, if you need to remove a temporary fi le you created, but perhaps you don’t need to do
it before replying to the client, you can defer it like this:

 stream.on("data", function(data) {
  stream.end("my response");
  process.nextTick(function() {
4     fs.unlink("/path/to/file");
5   });
});

nodejs(五)同步异步--BLOCKING THE EVENT LOOP的更多相关文章

  1. nodejs(五)同步异步--USING SETTIMEOUT INSTEAD OF SETINTERVAL TO FORCE SERIALIZATION

    Let’s say you want a function that does some I/O — such as parsing a log fi le — that will periodica ...

  2. Why should I avoid blocking the Event Loop and the Worker Pool?

    Don't Block the Event Loop (or the Worker Pool) | Node.js https://nodejs.org/en/docs/guides/dont-blo ...

  3. 不要在nodejs中阻塞event loop

    目录 简介 event loop和worker pool event loop和worker pool中的queue 阻塞event loop event loop的时间复杂度 Event Loop中 ...

  4. 并发编程--一堆锁,GIL,同步异步,Event事件

    目录 一堆锁 死锁现象(*****) 递归锁 RLock (了解) 信号量 (了解) GIL(*****) 什么时GIL锁 为什么需要GIL锁 Cpython解释器与GC的问题 GIL锁带来的问题 多 ...

  5. [转载]JavaScript 运行机制详解:再谈Event Loop

    https://app.yinxiang.com/shard/s8/sh/b72fe246-a89d-434b-85f0-a36420849b84/59bad790bdcf6b0a66b8b93d5e ...

  6. 【朴灵评注】JavaScript 运行机制详解:再谈Event Loop

    PS: 我先旁观下大师们的讨论,得多看书了~   别人说的:“看了一下不觉得评注对到哪里去,只有吹毛求疵之感. 比如同步异步介绍,本来就无大错:比如node图里面的OS operation,推敲一下就 ...

  7. {Python之进程} 背景知识 什么是进程 进程调度 并发与并行 同步\异步\阻塞\非阻塞 进程的创建与结束 multiprocess模块 进程池和mutiprocess.Poll

    Python之进程 进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mut ...

  8. [NodeJs系列][译]理解NodeJs中的Event Loop、Timers以及process.nextTick()

    译者注: 为什么要翻译?其实在翻译这篇文章前,笔者有Google了一下中文翻译,看的不是很明白,所以才有自己翻译的打算,当然能力有限,文中或有错漏,欢迎指正. 文末会有几个小问题,大家不妨一起思考一下 ...

  9. 理解Nodejs的Event Loop

    Node的“event loop”主要是用来处理高输出量的.这很神奇,这也是为什么node可以在单线程的情况下同时处理很多的后台操作.本文就会集中讲述event loop是怎么运行的,这样你可以可以使 ...

随机推荐

  1. Linux误删文件后恢复数据

    在Linux下,基于开源的数据恢复工具有很多,常见的有debugfs.R-Linux.ext3grep.extundelete等,比较常用的有ext3grep和extundelete,这两个工具的恢复 ...

  2. GitHub 在使用命令行 git push 时报错:The requested URL returned error: 403

    使用 git 的命令行向 GitHub 提交的时候,报错: [Young@localhost OtherLang]$ git push origin master error: The request ...

  3. C# 调用存储过程出错:String[3]: Size 属性具有无效大小值 0

    存储过程如下 Create PROCEDURE [dbo].[Test] @FundId int, @vchStrategyToken nvarchar(), @ErrorMessage nvarch ...

  4. 题目1003:A+B(按逗号分隔的A+B)

    题目链接:http://ac.jobdu.com/problem.php?pid=1003 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. sencha touch datepicker/datepickerfield(时间选择控件)扩展(废弃 仅参考)

    参考资料:https://market.sencha.com/extensions/datetimepicker 上面的扩展在2.2有些问题,参考源码重新写了一个 TimePicker: Ext.de ...

  6. backface-visibility 属性

    图片img加了backface-visibility 属性, 图片变清晰, 原因不明 a,img{ backface-visibility:hidden; -webkit-backface-visib ...

  7. phantomjs试玩

    简单来说,phantomjs就是一个运行在node上的webkit内核,支持DOM渲染,css选择器,Canvas,SVG等,在浏览器上能做的事情,理论上,phantomjs 都能模拟做到. phan ...

  8. yii---生产链接的方法

    yii生成链接的方法: Yii::$app->urlManager->createUrl('xxx/xxx') <?= Yii::$app->urlManager->cr ...

  9. 关于JavaScript转义字符('、 " 、\" 、\')【原创】

    先插入一条广告,博主新开了一家淘宝店,经营自己纯手工做的发饰,新店开业,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!店名: 小鱼尼莫手工饰品店经营: 发饰.头花.发夹.耳环等(手工制作)网店: ...

  10. 爬虫之requests详解

    requests Python标准库中提供了:urllib.urllib2.httplib等模块以供Http请求,但是,它的 API 太渣了.它是为另一个时代.另一个互联网所创建的.它需要巨量的工作, ...