book-exploring-async-basics
book-exploring-async-basics copied to clipboard
[Translation] 7_1_what_is_node.md
However, the part of Node that "progresses" your code does indeed run on a single thread.
progresshere is equalinterpret & compile. So if there is a blocking function called in the global scope, it means the event loop is blocked. Here is my understanding:
var rf=require("fs");
// not blocking
rf.readFile("test",'utf-8',function(err,data){
if(err){
console.log("error");
}else{
console.log(data);
}
// blocking
var data=rf.readFileSync("test","utf-8");
// not blocking because it's the synchronous read is not in the global scope
setTimeout(1000, () => {
var data=rf.readFileSync("test","utf-8");
})
I/O tasks which can't be handled by the cross platform event queue are also handled here, which is the case with file reads that we use in our example.
So event queue is actually used in network I/O.
Most C++ extensions for Node uses this thread pool to perform their work, and that is one of many reasons they are used for calculation-intensive tasks.
What's they here referring to, C++ extensions or thread pools? I can hardly understand this sentence, could you clarify it concretely, maybe with an example?
progress here is equal interpret & compile. So if there is a blocking function called in the global scope, it means the event loop is blocked. Here is my understanding:
Your understanding seems to be correct in the code example.
But the translation of "progress" to "interpret & compile" seems wrong. It's kind of important to distinguish the interpretation/compiling og JavaScript and node. V8 is the JavasScript interpreter/compiler. The node eventloop doesn't have anything with interpreting to do. Node uses V8 to compile JavaScript but that doesn't have anything to do with Node runtime and eventloop directly. What I meant with that sentence is that both the scheduler and actual execution of what you write in javascript is happens on the same thread.
So event queue is actually used in network I/O.
Yes, it's how Node can achieve such good performance.
What's they here referring to, C++ extensions or thread pools? I can hardly understand this sentence, could you clarify it concretely, maybe with an example?
Ok, so they here refers to C++ extensions which can use the threadpool. An example of this would be cryptography. Instead of running that in the same thread as the javascript code and event loop it sends that task to a different thread, notifying the eventloop when the work is done so it can run the callback that is registered to run once the crypto function is done.
I don't have a good example on the top of my head that I can easily show here but I believe it will be clearer when you get to chapter 8 and 9.
But the translation of "progress" to "interpret & compile" seems wrong. It's kind of important to distinguish the interpretation/compiling og JavaScript and node. V8 is the JavasScript interpreter/compiler. The node eventloop doesn't have anything with interpreting to do. Node uses V8 to compile JavaScript but that doesn't have anything to do with Node runtime and eventloop directly. What I meant with that sentence is that both the scheduler and actual execution of what you write in javascript is happens on the same thread.
I see. Then how about translate "progress the code" into "push/drive the code to be executed", for "progress" is mostly used as a noun and it's hard for translation.