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 ab 命令

    ab 是一个性能测试工具,用来测试一个页面每秒钟能处理多少HTTP请求 [root@localhost ~]$ yum install -y httpd-tools # 安装ab工具 [root@lo ...

  2. [Git] 解决 insufficient permission for adding an object to repository database

    [环境] OS: CentOS 6.5 Git: 1.7.1 [症状描述] Git 中心仓库路径 ~/project.git,克隆库路径 ~/project.clone,克隆库中包含一个文件 ~/pr ...

  3. mysql check约束无效

    转自http://blog.csdn.net/maxint64/article/details/8643288 今天在mysql中尝试使用check约束时,才知道在MySQL中CHECK约束是无效的, ...

  4. PHP 连接oracle

    function connect_oracle(){ static $dbconn = false; if(!$dbconn){ $db_server = "127.0.0.1"; ...

  5. vim 正则替换【转】

    :[range]s/from/to/[flags] range:搜索范围,如果没有指定范围,则作用于但前行. :1,10s/from/to/ 表示在第1到第10行(包含第1,第10行)之间搜索替换: ...

  6. 列式存储hbase系统架构学习

    一.Hbase简介 HBase是一个开源的非关系型分布式数据库(NoSQL),它参考了谷歌的BigTable建模,实现的编程语言为 Java.它是Apache软件基金会的Hadoop项目的一部分,运行 ...

  7. C++ #include—尖括号和双引号的区别

    如果你还看一些别的C++教程,那么你可能很早就发现了,有些书上的#include命令写作#include <文件名>,但有时候又会出现#include "文件名".你会 ...

  8. VScode首选项

    首选项相关的设置: // 将设置放入此文件中以覆盖默认设置 { "workbench.iconTheme": "vscode-icons", "wor ...

  9. remote: fatal: could not read Username for 'http://spapa.wicp.net:3000': No such device ors

    解决办法: git remote add origin https://{username}:{password}@github.com/{username}/project.git in my ca ...

  10. ms转成00:00:00的时间格式化

    毫秒转成 00:00:00的时间格式 比如1000毫秒转成00:00:01 /** * 格式化邀请的时间 * @param time ms */ public static formatTime(ti ...