mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
483 lines
17 KiB
483 lines
17 KiB
9 years ago
|
# The Node.js Event Loop, Timers, and `process.nextTick()`
|
||
|
|
||
|
## What is the Event Loop?
|
||
|
|
||
|
The event loop is what allows Node.js to perform non-blocking I/O
|
||
|
operations — despite the fact that JavaScript is single-threaded — by
|
||
9 years ago
|
offloading operations to the system kernel whenever possible.
|
||
9 years ago
|
|
||
|
Since most modern kernels are multi-threaded, they can handle multiple
|
||
|
operations executing in the background. When one of these operations
|
||
|
completes, the kernel tells Node.js so that the appropriate callback
|
||
9 years ago
|
may added to the **poll** queue to eventually be executed. We'll explain
|
||
9 years ago
|
this in further detail later in this topic.
|
||
|
|
||
9 years ago
|
## Event Loop Explained
|
||
9 years ago
|
|
||
|
When Node.js starts, it initializes the event loop, processes the
|
||
|
provided input script (or drops into the REPL, which is not covered in
|
||
|
this document) which may make async API calls, schedule timers, or call
|
||
9 years ago
|
`process.nextTick()`, then begins processing the event loop.
|
||
9 years ago
|
|
||
|
The following diagram shows a simplified overview of the event loop's
|
||
|
order of operations.
|
||
|
|
||
8 years ago
|
```txt
|
||
|
┌───────────────────────┐
|
||
|
┌─>│ timers │
|
||
|
│ └──────────┬────────────┘
|
||
|
│ ┌──────────┴────────────┐
|
||
|
│ │ I/O callbacks │
|
||
|
│ └──────────┬────────────┘
|
||
|
│ ┌──────────┴────────────┐
|
||
|
│ │ idle, prepare │
|
||
|
│ └──────────┬────────────┘ ┌───────────────┐
|
||
|
│ ┌──────────┴────────────┐ │ incoming: │
|
||
|
│ │ poll │<─────┤ connections, │
|
||
|
│ └──────────┬────────────┘ │ data, etc. │
|
||
|
│ ┌──────────┴────────────┐ └───────────────┘
|
||
|
│ │ check │
|
||
|
│ └──────────┬────────────┘
|
||
|
│ ┌──────────┴────────────┐
|
||
|
└──┤ close callbacks │
|
||
|
└───────────────────────┘
|
||
|
```
|
||
9 years ago
|
|
||
|
*note: each box will be referred to as a "phase" of the event loop.*
|
||
|
|
||
|
Each phase has a FIFO queue of callbacks to execute. While each phase is
|
||
|
special in its own way, generally, when the event loop enters a given
|
||
|
phase, it will perform any operations specific to that phase, then
|
||
|
execute callbacks in that phase's queue until the queue has been
|
||
9 years ago
|
exhausted or the maximum number of callbacks has executed. When the
|
||
9 years ago
|
queue has been exhausted or the callback limit is reached, the event
|
||
|
loop will move to the next phase, and so on.
|
||
|
|
||
|
Since any of these operations may schedule _more_ operations and new
|
||
9 years ago
|
events processed in the **poll** phase are queued by the kernel, poll
|
||
9 years ago
|
events can be queued while polling events are being processed. As a
|
||
|
result, long running callbacks can allow the poll phase to run much
|
||
9 years ago
|
longer than a timer's threshold. See the [**timers**](#timers) and
|
||
|
[**poll**](#poll) sections for more details.
|
||
9 years ago
|
|
||
|
_**NOTE:** There is a slight discrepancy between the Windows and the
|
||
|
Unix/Linux implementation, but that's not important for this
|
||
|
demonstration. The most important parts are here. There are actually
|
||
|
seven or eight steps, but the ones we care about — ones that Node.js
|
||
9 years ago
|
actually uses - are those above._
|
||
9 years ago
|
|
||
|
|
||
9 years ago
|
## Phases Overview
|
||
9 years ago
|
|
||
9 years ago
|
* **timers**: this phase executes callbacks scheduled by `setTimeout()`
|
||
9 years ago
|
and `setInterval()`.
|
||
8 years ago
|
* **I/O callbacks**: executes almost all callbacks with the exception of
|
||
|
close callbacks, the ones scheduled by timers, and `setImmediate()`.
|
||
|
* **idle, prepare**: only used internally.
|
||
|
* **poll**: retrieve new I/O events; node will block here when appropriate.
|
||
|
* **check**: `setImmediate()` callbacks are invoked here.
|
||
|
* **close callbacks**: e.g. `socket.on('close', ...)`.
|
||
9 years ago
|
|
||
|
Between each run of the event loop, Node.js checks if it is waiting for
|
||
9 years ago
|
any asynchronous I/O or timers and shuts down cleanly if there are not
|
||
9 years ago
|
any.
|
||
|
|
||
|
## Phases in Detail
|
||
|
|
||
|
### timers
|
||
|
|
||
|
A timer specifies the **threshold** _after which_ a provided callback
|
||
|
_may be executed_ rather than the **exact** time a person _wants it to
|
||
|
be executed_. Timers callbacks will run as early as they can be
|
||
|
scheduled after the specified amount of time has passed; however,
|
||
|
Operating System scheduling or the running of other callbacks may delay
|
||
9 years ago
|
them.
|
||
9 years ago
|
|
||
9 years ago
|
_**Note**: Technically, the [**poll** phase](#poll) controls when timers
|
||
9 years ago
|
are executed._
|
||
|
|
||
9 years ago
|
For example, say you schedule a timeout to execute after a 100 ms
|
||
|
threshold, then your script starts asynchronously reading a file which
|
||
9 years ago
|
takes 95 ms:
|
||
|
|
||
|
```js
|
||
|
|
||
|
var fs = require('fs');
|
||
|
|
||
|
function someAsyncOperation (callback) {
|
||
9 years ago
|
// Assume this takes 95ms to complete
|
||
9 years ago
|
fs.readFile('/path/to/file', callback);
|
||
|
}
|
||
|
|
||
|
var timeoutScheduled = Date.now();
|
||
|
|
||
|
setTimeout(function () {
|
||
|
|
||
|
var delay = Date.now() - timeoutScheduled;
|
||
|
|
||
|
console.log(delay + "ms have passed since I was scheduled");
|
||
|
}, 100);
|
||
|
|
||
|
|
||
|
// do someAsyncOperation which takes 95 ms to complete
|
||
|
someAsyncOperation(function () {
|
||
|
|
||
|
var startCallback = Date.now();
|
||
|
|
||
|
// do something that will take 10ms...
|
||
|
while (Date.now() - startCallback < 10) {
|
||
|
; // do nothing
|
||
|
}
|
||
|
|
||
|
});
|
||
|
```
|
||
|
|
||
9 years ago
|
When the event loop enters the **poll** phase, it has an empty queue
|
||
|
(`fs.readFile()` has not completed), so it will wait for the number of ms
|
||
9 years ago
|
remaining until the soonest timer's threshold is reached. While it is
|
||
|
waiting 95 ms pass, `fs.readFile()` finishes reading the file and its
|
||
9 years ago
|
callback which takes 10 ms to complete is added to the **poll** queue and
|
||
9 years ago
|
executed. When the callback finishes, there are no more callbacks in the
|
||
|
queue, so the event loop will see that the threshold of the soonest
|
||
9 years ago
|
timer has been reached then wrap back to the **timers** phase to execute
|
||
9 years ago
|
the timer's callback. In this example, you will see that the total delay
|
||
9 years ago
|
between the timer being scheduled and its callback being executed will
|
||
9 years ago
|
be 105ms.
|
||
|
|
||
9 years ago
|
Note: To prevent the **poll** phase from starving the event loop, libuv
|
||
|
also has a hard maximum (system dependent) before it stops polling for
|
||
9 years ago
|
more events.
|
||
|
|
||
9 years ago
|
### I/O callbacks
|
||
9 years ago
|
|
||
9 years ago
|
This phase executes callbacks for some system operations such as types
|
||
9 years ago
|
of TCP errors. For example if a TCP socket receives `ECONNREFUSED` when
|
||
|
attempting to connect, some \*nix systems want to wait to report the
|
||
9 years ago
|
error. This will be queued to execute in the **I/O callbacks** phase.
|
||
9 years ago
|
|
||
9 years ago
|
### poll
|
||
9 years ago
|
|
||
9 years ago
|
The **poll** phase has two main functions:
|
||
9 years ago
|
|
||
9 years ago
|
1. Executing scripts for timers whose threshold has elapsed, then
|
||
|
2. Processing events in the **poll** queue.
|
||
9 years ago
|
|
||
9 years ago
|
When the event loop enters the **poll** phase _and there are no timers
|
||
9 years ago
|
scheduled_, one of two things will happen:
|
||
|
|
||
9 years ago
|
* _If the **poll** queue **is not empty**_, the event loop will iterate
|
||
|
through its queue of callbacks executing them synchronously until
|
||
|
either the queue has been exhausted, or the system-dependent hard limit
|
||
9 years ago
|
is reached.
|
||
|
|
||
9 years ago
|
* _If the **poll** queue **is empty**_, one of two more things will
|
||
9 years ago
|
happen:
|
||
9 years ago
|
* If scripts have been scheduled by `setImmediate()`, the event loop
|
||
9 years ago
|
will end the **poll** phase and continue to the **check** phase to
|
||
9 years ago
|
execute those scheduled scripts.
|
||
|
|
||
|
* If scripts **have not** been scheduled by `setImmediate()`, the
|
||
|
event loop will wait for callbacks to be added to the queue, then
|
||
9 years ago
|
execute them immediately.
|
||
9 years ago
|
|
||
9 years ago
|
Once the **poll** queue is empty the event loop will check for timers
|
||
9 years ago
|
_whose time thresholds have been reached_. If one or more timers are
|
||
9 years ago
|
ready, the event loop will wrap back to the **timers** phase to execute
|
||
9 years ago
|
those timers' callbacks.
|
||
|
|
||
9 years ago
|
### check
|
||
9 years ago
|
|
||
9 years ago
|
This phase allows a person to execute callbacks immediately after the
|
||
|
**poll** phase has completed. If the **poll** phase becomes idle and
|
||
|
scripts have been queued with `setImmediate()`, the event loop may
|
||
|
continue to the **check** phase rather than waiting.
|
||
9 years ago
|
|
||
|
`setImmediate()` is actually a special timer that runs in a separate
|
||
|
phase of the event loop. It uses a libuv API that schedules callbacks to
|
||
9 years ago
|
execute after the **poll** phase has completed.
|
||
9 years ago
|
|
||
|
Generally, as the code is executed, the event loop will eventually hit
|
||
9 years ago
|
the **poll** phase where it will wait for an incoming connection, request,
|
||
|
etc. However, if a callback has been scheduled with `setImmediate()`
|
||
|
and the **poll** phase becomes idle, it will end and continue to the
|
||
|
**check** phase rather than waiting for **poll** events.
|
||
9 years ago
|
|
||
9 years ago
|
### close callbacks
|
||
9 years ago
|
|
||
|
If a socket or handle is closed abruptly (e.g. `socket.destroy()`), the
|
||
|
`'close'` event will be emitted in this phase. Otherwise it will be
|
||
|
emitted via `process.nextTick()`.
|
||
|
|
||
|
## `setImmediate()` vs `setTimeout()`
|
||
|
|
||
|
`setImmediate` and `setTimeout()` are similar, but behave in different
|
||
9 years ago
|
ways depending on when they are called.
|
||
9 years ago
|
|
||
|
* `setImmediate()` is designed to execute a script once the current
|
||
9 years ago
|
**poll** phase completes.
|
||
|
* `setTimeout()` schedules a script to be run after a minimum threshold
|
||
|
in ms has elapsed.
|
||
9 years ago
|
|
||
9 years ago
|
The order in which the timers are executed will vary depending on the
|
||
9 years ago
|
context in which they are called. If both are called from within the
|
||
9 years ago
|
main module, then timing will be bound by the performance of the process
|
||
|
(which can be impacted by other applications running on the machine).
|
||
|
|
||
|
For example, if we run the following script which is not within an I/O
|
||
|
cycle (i.e. the main module), the order in which the two timers are
|
||
|
executed is non-deterministic, as it is bound by the performance of the
|
||
|
process:
|
||
9 years ago
|
|
||
|
|
||
|
```js
|
||
|
// timeout_vs_immediate.js
|
||
|
setTimeout(function timeout () {
|
||
|
console.log('timeout');
|
||
|
},0);
|
||
|
|
||
|
setImmediate(function immediate () {
|
||
|
console.log('immediate');
|
||
|
});
|
||
|
```
|
||
|
|
||
8 years ago
|
```console
|
||
|
$ node timeout_vs_immediate.js
|
||
|
timeout
|
||
|
immediate
|
||
9 years ago
|
|
||
8 years ago
|
$ node timeout_vs_immediate.js
|
||
|
immediate
|
||
|
timeout
|
||
|
```
|
||
9 years ago
|
|
||
|
However, if you move the two calls within an I/O cycle, the immediate
|
||
|
callback is always executed first:
|
||
|
|
||
|
```js
|
||
|
// timeout_vs_immediate.js
|
||
|
var fs = require('fs')
|
||
|
|
||
|
fs.readFile(__filename, () => {
|
||
|
setTimeout(() => {
|
||
|
console.log('timeout')
|
||
|
}, 0)
|
||
|
setImmediate(() => {
|
||
|
console.log('immediate')
|
||
|
})
|
||
|
})
|
||
|
```
|
||
|
|
||
8 years ago
|
```console
|
||
|
$ node timeout_vs_immediate.js
|
||
|
immediate
|
||
|
timeout
|
||
9 years ago
|
|
||
8 years ago
|
$ node timeout_vs_immediate.js
|
||
|
immediate
|
||
|
timeout
|
||
|
```
|
||
9 years ago
|
|
||
|
The main advantage to using `setImmediate()` over `setTimeout()` is
|
||
|
`setImmediate()` will always be executed before any timers if scheduled
|
||
|
within an I/O cycle, independently of how many timers are present.
|
||
|
|
||
9 years ago
|
## `process.nextTick()`
|
||
9 years ago
|
|
||
|
### Understanding `process.nextTick()`
|
||
|
|
||
|
You may have noticed that `process.nextTick()` was not displayed in the
|
||
9 years ago
|
diagram, even though it's a part of the asynchronous API. This is because
|
||
9 years ago
|
`process.nextTick()` is not technically part of the event loop. Instead,
|
||
9 years ago
|
the `nextTickQueue` will be processed after the current operation
|
||
|
completes, regardless of the current phase of the event loop.
|
||
9 years ago
|
|
||
|
Looking back at our diagram, any time you call `process.nextTick()` in a
|
||
|
given phase, all callbacks passed to `process.nextTick()` will be
|
||
|
resolved before the event loop continues. This can create some bad
|
||
|
situations because **it allows you to "starve" your I/O by making
|
||
9 years ago
|
recursive `process.nextTick()` calls**, which prevents the event loop
|
||
|
from reaching the **poll** phase.
|
||
9 years ago
|
|
||
|
### Why would that be allowed?
|
||
|
|
||
|
Why would something like this be included in Node.js? Part of it is a
|
||
|
design philosophy where an API should always be asynchronous even where
|
||
|
it doesn't have to be. Take this code snippet for example:
|
||
|
|
||
|
```js
|
||
|
function apiCall (arg, callback) {
|
||
|
if (typeof arg !== 'string')
|
||
|
return process.nextTick(callback,
|
||
|
new TypeError('argument should be string'));
|
||
|
}
|
||
|
```
|
||
|
|
||
|
The snippet does an argument check and if it's not correct, it will pass
|
||
|
the error to the callback. The API updated fairly recently to allow
|
||
|
passing arguments to `process.nextTick()` allowing it to take any
|
||
|
arguments passed after the callback to be propagated as the arguments to
|
||
|
the callback so you don't have to nest functions.
|
||
|
|
||
|
What we're doing is passing an error back to the user but only *after*
|
||
|
we have allowed the rest of the user's code to execute. By using
|
||
|
`process.nextTick()` we guarantee that `apiCall()` always runs its
|
||
|
callback *after* the rest of the user's code and *before* the event loop
|
||
9 years ago
|
is allowed to proceed. To achieve this, the JS call stack is allowed to
|
||
9 years ago
|
unwind then immediately execute the provided callback which allows a
|
||
9 years ago
|
person to make recursive calls to `process.nextTick()` without reaching a
|
||
9 years ago
|
`RangeError: Maximum call stack size exceeded from v8`.
|
||
|
|
||
|
This philosophy can lead to some potentially problematic situations.
|
||
|
Take this snippet for example:
|
||
|
|
||
|
```js
|
||
|
// this has an asynchronous signature, but calls callback synchronously
|
||
|
function someAsyncApiCall (callback) { callback(); };
|
||
|
|
||
|
// the callback is called before `someAsyncApiCall` completes.
|
||
|
someAsyncApiCall(() => {
|
||
|
|
||
|
// since someAsyncApiCall has completed, bar hasn't been assigned any value
|
||
|
console.log('bar', bar); // undefined
|
||
|
|
||
|
});
|
||
|
|
||
|
var bar = 1;
|
||
|
```
|
||
|
|
||
|
The user defines `someAsyncApiCall()` to have an asynchronous signature,
|
||
9 years ago
|
but it actually operates synchronously. When it is called, the callback
|
||
|
provided to `someAsyncApiCall()` is called in the same phase of the
|
||
9 years ago
|
event loop because `someAsyncApiCall()` doesn't actually do anything
|
||
9 years ago
|
asynchronously. As a result, the callback tries to reference `bar` even
|
||
|
though it may not have that variable in scope yet, because the script has not
|
||
9 years ago
|
been able to run to completion.
|
||
|
|
||
9 years ago
|
By placing the callback in a `process.nextTick()`, the script still has the
|
||
9 years ago
|
ability to run to completion, allowing all the variables, functions,
|
||
9 years ago
|
etc., to be initialized prior to the callback being called. It also has
|
||
9 years ago
|
the advantage of not allowing the event loop to continue. It may be
|
||
9 years ago
|
useful for the user to be alerted to an error before the event loop is
|
||
|
allowed to continue. Here is the previous example using `process.nextTick()`:
|
||
|
|
||
|
```js
|
||
|
function someAsyncApiCall (callback) {
|
||
|
process.nextTick(callback);
|
||
|
};
|
||
|
|
||
|
someAsyncApiCall(() => {
|
||
|
console.log('bar', bar); // 1
|
||
|
});
|
||
|
|
||
|
var bar = 1;
|
||
|
```
|
||
9 years ago
|
|
||
9 years ago
|
Here's another real world example:
|
||
9 years ago
|
|
||
|
```js
|
||
|
const server = net.createServer(() => {}).listen(8080);
|
||
|
|
||
|
server.on('listening', () => {});
|
||
|
```
|
||
|
|
||
|
When only a port is passed the port is bound immediately. So the
|
||
|
`'listening'` callback could be called immediately. Problem is that the
|
||
9 years ago
|
`.on('listening')` will not have been set by that time.
|
||
9 years ago
|
|
||
|
To get around this the `'listening'` event is queued in a `nextTick()`
|
||
9 years ago
|
to allow the script to run to completion. Which allows the user to set
|
||
9 years ago
|
any event handlers they want.
|
||
|
|
||
|
## `process.nextTick()` vs `setImmediate()`
|
||
|
|
||
|
We have two calls that are similar as far as users are concerned, but
|
||
|
their names are confusing.
|
||
|
|
||
|
* `process.nextTick()` fires immediately on the same phase
|
||
9 years ago
|
* `setImmediate()` fires on the following iteration or 'tick' of the
|
||
9 years ago
|
event loop
|
||
|
|
||
|
In essence, the names should be swapped. `process.nextTick()` fires more
|
||
|
immediately than `setImmediate()` but this is an artifact of the past
|
||
|
which is unlikely to change. Making this switch would break a large
|
||
|
percentage of the packages on npm. Every day more new modules are being
|
||
|
added, which mean every day we wait, more potential breakages occur.
|
||
|
While they are confusing, the names themselves won't change.
|
||
|
|
||
9 years ago
|
*We recommend developers use `setImmediate()` in all cases because it's
|
||
9 years ago
|
easier to reason about (and it leads to code that's compatible with a
|
||
|
wider variety of environments, like browser JS.)*
|
||
|
|
||
9 years ago
|
## Why use `process.nextTick()`?
|
||
9 years ago
|
|
||
|
There are two main reasons:
|
||
|
|
||
|
1. Allow users to handle errors, cleanup any then unneeded resources, or
|
||
|
perhaps try the request again before the event loop continues.
|
||
|
|
||
|
2. At times it's necessary to allow a callback to run after the call
|
||
|
stack has unwound but before the event loop continues.
|
||
|
|
||
|
One example is to match the user's expectations. Simple example:
|
||
|
|
||
|
```js
|
||
|
var server = net.createServer();
|
||
|
server.on('connection', function(conn) { });
|
||
|
|
||
|
server.listen(8080);
|
||
|
server.on('listening', function() { });
|
||
|
```
|
||
|
|
||
9 years ago
|
Say that `listen()` is run at the beginning of the event loop, but the
|
||
9 years ago
|
listening callback is placed in a `setImmediate()`. Now, unless a
|
||
9 years ago
|
hostname is passed binding to the port will happen immediately. Now for
|
||
|
the event loop to proceed it must hit the **poll** phase, which means
|
||
|
there is a non-zero chance that a connection could have been received
|
||
9 years ago
|
allowing the connection event to be fired before the listening event.
|
||
|
|
||
9 years ago
|
Another example is running a function constructor that was to, say,
|
||
9 years ago
|
inherit from `EventEmitter` and it wanted to call an event within the
|
||
|
constructor:
|
||
|
|
||
|
```js
|
||
|
const EventEmitter = require('events');
|
||
|
const util = require('util');
|
||
|
|
||
|
function MyEmitter() {
|
||
|
EventEmitter.call(this);
|
||
|
this.emit('event');
|
||
|
}
|
||
|
util.inherits(MyEmitter, EventEmitter);
|
||
|
|
||
|
const myEmitter = new MyEmitter();
|
||
|
myEmitter.on('event', function() {
|
||
|
console.log('an event occurred!');
|
||
|
});
|
||
|
```
|
||
|
|
||
9 years ago
|
You can't emit an event from the constructor immediately
|
||
|
because the script will not have processed to the point where the user
|
||
|
assigns a callback to that event. So, within the constructor itself,
|
||
|
you can use `process.nextTick()` to set a callback to emit the event
|
||
9 years ago
|
after the constructor has finished, which provides the expected results:
|
||
|
|
||
|
```js
|
||
|
const EventEmitter = require('events');
|
||
|
const util = require('util');
|
||
|
|
||
|
function MyEmitter() {
|
||
|
EventEmitter.call(this);
|
||
|
|
||
|
// use nextTick to emit the event once a handler is assigned
|
||
|
process.nextTick(function () {
|
||
|
this.emit('event');
|
||
|
}.bind(this));
|
||
|
}
|
||
|
util.inherits(MyEmitter, EventEmitter);
|
||
|
|
||
|
const myEmitter = new MyEmitter();
|
||
|
myEmitter.on('event', function() {
|
||
|
console.log('an event occurred!');
|
||
|
});
|
||
|
```
|