This is from the book NodeJS in a Simple Language
Why is it said that asynchronous time-consuming depends on the slowest file?
Even if it is asynchronous, isn't it single-threaded? It needs to complete one task before executing the next one.
It depends on the slowest file. Doesn't it mean these two? Are the codes executed at the same time?
我想大声告诉你2017-05-31 10:41:42
同步I/O 是 1->2->3->4 这样的时间,为1+2+3+4的时间
异步I/O 是 --> 1
2
3
4
四个同时开始 时间为4个里面最慢的那个。。
There is also js single thread. The basic js code you write is single thread. Once it is basic network or file I/O, it will enter the event loop and perform concurrent operations. . . 1234 started at the same time. No one waits for anyone.
过去多啦不再A梦2017-05-31 10:41:42
The bottom layer of node.js asynchronous tasks is implemented by multi-threading. For an asynchronous task, node maintains a thread pool. An asynchronous task takes out a thread from the thread pool for execution. After execution, the thread is returned to the thread pool
淡淡烟草味2017-05-31 10:41:42
Simultaneous and synchronized are two different things! Synchronization refers to the order in which things are executed! The latter must rely on the results of the former! Asynchronous is performed at the same time, but the connection between things has little impact on the entire process! For example, if you make multiple ajax requests at the same time, your purpose is just to request the data and display that there is no correlation between the data. Let’s use an analogy! It’s like building a road! Definitely lay the asphalt first and then paint the lines! Otherwise, you will mess up the paved road by drawing lines while paving it! At this time, it will be a mess if it must be synchronous and asynchronous! However, line marking, road cleaning, and road fence installation can be done at the same time. This is called asynchronous and will not affect the results, but it will also be slower because the installation of fences is more complex than other operations. But in the end, the entire process was completed. ! On the other hand, if you process the latter three simultaneously, the waiting time will be longer because you have to wait for the previous step to be processed before processing the next step! But in the past, 10 people were building roads. Since there were three things to deal with in the later stage, if we wanted to achieve asynchronous implementation, we had to increase it to 30 people. It was like we increased the cost of computers (not very professional here), but there are often excess computing resources. We can make good use of asynchronous in this way. Computing resources!
阿神2017-05-31 10:41:42
A program can use multiple CPU cores to execute multiple tasks in parallel at the same time. The bottom layer is either multi-threaded or multi-process.
The bottom layer of Node is libuv.
For network reading and writing (I/O), libuv uses the epoll mechanism provided by the kernel on Linux. The bottom layer of epoll in the Linux kernel should be implemented using kernel threads, but the bottom layer implementation of epoll does not require libuv to care, libuv only needs to Call.
For file reading and writing (I/O), libuv may use Linux AIO, or it may use multi-threading to implement it. Because the asynchronous file reading and writing mechanism AIO provided by the Linux kernel mainly serves the database, for example, MySQL relies on libaio.so .The current Kernel AIO only supports O_DIRECT mode (DirectIO) to read and write to the disk. This means that the program cannot use the system's cache Page Cache, and it requires the size and offset of the read and write to be aligned in a block manner. It may be fine for reading and writing large files, but it is not suitable for small files. So libuv should use multi-threading at the bottom layer to implement asynchronous file reading and writing.
Whether it is network reading and writing or file reading and writing, the program always returns. When it returns, it must return after all tasks are completed, so the time-consuming task is generally the task that takes the longest to read and write. However, file reading and writing It may be limited by disk IOPS, which will inevitably cause congestion, and the overall time consumption may be longer.
When Node is used for server programming or server middle-tier programming, network reading and writing mainly occur.