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. CharacterMotor_刚体角色驱动

    using UnityEngine; //this class holds movement functions for a rigidbody character such as player, e ...

  2. MQ java 基础编程(一)

    本文转自:http://www.blogjava.net/i369/articles/88035.html 编写人:邬文俊 编写时间 : 2006-2-16 联系邮件 : wenjunwu430@gm ...

  3. mysql基础知识笔记

    Mysql基础笔记 环境配置 基本概念 mysql命令行 进入 use show 查询 select order by where like 数据过滤regexp concat 文本函数 日期函数 数 ...

  4. 节日换肤通用技术方案__iOS端实现

    一.问题的提出 不知道大家有没有发现, 元旦期间, 很多APP界面里的图标都换成了具有节日气氛的样式, 而在过了元旦节之后, 这些图标又悄无声息的变回了本来的面貌. 这些具有短暂生命周期.而又必须在固 ...

  5. x64免签名驱动程序

    DSEFix GitHub https://github.com/hfiref0x/DSEFix

  6. EGit系列第二篇——关联远程仓库

    网上也有很多代码托管网站支持git,像最出名的GitHub,还有国内支持私有项目的OSC开源中国和CSDN等... 首先得注册个帐号,然后才可以创建仓库 一般都会带一个ReadMe.md,你可以勾选也 ...

  7. shell 获取脚本的绝对路径

    basepath=$(cd `dirname $0`; pwd) 在此解释下basepath : dirname $0,取得当前执行的脚本文件的父目录 cd `dirname $0`,进入这个目录(切 ...

  8. 剑指offer题目记录

    1.如下为类型CMyString的声明,请为该类型添加赋值运算符函数. class CMyString { public: CMyString(char* pData = NULL); CMyStri ...

  9. 23种设计模式之组合模式(Composite)

    组合模式又称为整体-部分(Part-whole)模式,属于对象的结构模式.在组合模式中,通过组合多个对象形成树形结构以表示整体-部分的结构层次.组合模式对单个对象(即叶子对象)和组合对象(即容器对象) ...

  10. cadence allegro 封装焊盘编号修改 (引脚编号修改)

    1. 打开dra文件在find里面 off all  然后只点击text 2.点击需要更改的焊盘 3.菜单栏edit - text 4.弹出窗口修改即可 注意: 按照网上的其他操作并没有执行步骤1操作 ...