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. Github上star数超1000的Android列表控件

    Android开发中,列表估计是最最常使用到的控件之一了.列表相关的交互如下拉刷新,上拉更多,滑动菜单,拖动排序,滑动菜单,sticky header分组,FAB等等都是十分常见的体验.Github中 ...

  2. react native (一)

    开始接触app方面的工作,真心塞~ 又开始了周而复始的死磕一个问题专坐一整天的节奏,关键是还没有成绩,实在无语.╮(╯▽╰)╭,还是总结一下最近心塞历程吧-- react native中文网:http ...

  3. gcc/g++基本命令简介

    gcc & g++现在是gnu中最主要和最流行的c & c++编译器 .g++是c++的命令,以.cpp为主,对于c语言后缀名一般为.c.这时候命令换做gcc即可.其实是无关紧要的.其 ...

  4. 百度地图API接口

    js <script type="text/javascript"> // 百度地图API功能 var map = new BMap.Map("map&quo ...

  5. Material Design系列第二篇——Getting Started

    Getting Started This lesson teaches you to Apply the Material Theme Design Your Layouts Specify Elev ...

  6. 【本周主题】第一期:JavaScript单线程与异步

    相信下边这个图一定都不陌生,本周就围绕这张图深入了解下js代码执行时的来龙去脉. 一.JavaScript是单线程的 2018-11-19 21:21:21 周一 js本质是单线程的.这一特性是jav ...

  7. 二分法求平方根(Python实现)

    使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...

  8. Linux 安装Ruby详解(在线和离线安装)

    很多时候我们会发现,真实的生成环境很多都没有外网,只有内网环境,这个时候我们又需要安装Ruby,则不能提供yum命令进行在线安装了,这个时候我们就需要下载安装包进行离线安装.本文主要简单介绍如果离线安 ...

  9. Linux SVN 服务器

    一. SVN 简介 Subversion(SVN) 是一个开源的版本控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository) 中 ...

  10. Android Usb Camera HAL框架