-
-
Notifications
You must be signed in to change notification settings - Fork 35.4k
doc: topic blocking vs non-blocking #5326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
additional clarifications
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,22 +14,25 @@ network supported by [libuv](http://libuv.org/). | |
| **Blocking** is when the execution of additional JavaScript in the Node.js | ||
| process must wait until a non-JavaScript operation completes. This happens | ||
| because the event loop is unable to continue running JavaScript while a | ||
| **blocking** operation is executing. | ||
| **blocking** operation is occurring. | ||
|
|
||
| In Node.js, JavaScript that exhibits poor performance due to being CPU intensive | ||
| rather than waiting on a non-JavaScript operation, such as I/O, isn't typically | ||
| referred to as **blocking**. Synchronous methods in the Node.js standard library | ||
| that use libuv are the most commonly used **blocking** operations. Native | ||
| modules may also have **blocking** methods. | ||
|
|
||
| All of the I/O methods in the Node.js standard libraries provide asynchronous | ||
| All of the I/O methods in the Node.js standard library provide asynchronous | ||
| versions, which are **non-blocking**, and accept callback functions. Some | ||
| methods also have **blocking** counterparts, which usually have names that end | ||
| with `Sync`. | ||
| methods also have **blocking** counterparts, which have names that end with | ||
| `Sync`. | ||
|
|
||
|
|
||
| ## Comparing Code | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we |
||
|
|
||
| **Blocking** methods execute **synchronously** and **non-blocking** methods | ||
| execute **asynchronously**. | ||
|
|
||
| Using the File System module as an example, this is a **synchronous** file read: | ||
|
|
||
| ```js | ||
|
|
@@ -46,11 +49,11 @@ fs.readFile('/file.md', (err, data) => { | |
| }); | ||
| ``` | ||
|
|
||
| The first example appears simpler but it has the disadvantage of the second line | ||
| **blocking** the execution of any additional JavaScript until the entire file is | ||
| read. Note that in the synchronous version if an error is thrown it will need to | ||
| be caught or the process will crash. In the asynchronous version, it is up to | ||
| the author to decide whether an error should throw as shown. | ||
| The first example appears simpler than the second but has the disadvantage of | ||
| the second line **blocking** the execution of any additional JavaScript until | ||
| the entire file is read. Note that in the synchronous version if an error is | ||
| thrown it will need to be caught or the process will crash. In the asynchronous version, it is up to the author to decide whether an error should throw as | ||
| shown. | ||
|
|
||
| Let's expand our example a little bit: | ||
|
|
||
|
|
@@ -76,16 +79,16 @@ In the first example above, `console.log` will be called before `moreWork()`. In | |
| the second example `fs.readFile()` is **non-blocking** so JavaScript execution | ||
| can continue and `moreWork()` will be called first. The ability to run | ||
| `moreWork()` without waiting for the file read to complete is a key design | ||
| choice that allows for higher throughout. | ||
| choice that allows for higher throughput. | ||
|
|
||
|
|
||
| ## Concurrency and Throughput | ||
|
|
||
| JavaScript execution in Node.js is single threaded, so concurrency refers to the | ||
| event loop's capacity to execute JavaScript callback functions after completing | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. capacity or capability?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. capacity IMO There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we mention internal thread pool here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @YuriSolovyov might be better to leave out internal thread pool here unless you think its exclusion makes the statement here false. I think explaining it in another topic where it can receive more depth would be very valuable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just wanted to somehow stress that callbacks are still called sequentially and not concurrently. |
||
| other work. Any code that is expected to process requests in a concurrent manner | ||
| depends on the ability of the event loop to continue running as non-JavaScript | ||
| operations like I/O are happening. | ||
| other work. Any code that is expected to run in a concurrent manner must allow | ||
| the event loop to continue running as non-JavaScript operations, like I/O, are | ||
| occurring. | ||
|
|
||
| As an example, let's consider a case where each request to a web server takes | ||
| 50ms to complete and 45ms of that 50ms is database I/O that can be done | ||
|
|
@@ -125,7 +128,7 @@ const fs = require('fs'); | |
| fs.readFile('/file.md', (err, data) => { | ||
| if (err) throw err; | ||
| console.log(data); | ||
| fs.unlink('/file.md', err => { | ||
| fs.unlink('/file.md', (err) => { | ||
| if (err) throw err; | ||
| }); | ||
| }); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this needs to be clarified. For example,
will block and it is a JavaScript operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is a clarification to this a couple sentences later