Let’s first look at a few common statements
Let’s analyze these in detail Before saying yes and why, let’s do some preparatory work
Multi-process web application example pseudo code
listenFd = new Socket(); // 创建监听socket Bind(listenFd, 80); // 绑定端口 Listen(listenFd); // 开始监听 for ( ; ; ) { // 接收客户端请求,通过新的socket建立连接 connFd = Accept(listenFd); // fork子进程 if ((pid = Fork()) === 0) { // 子进程中 // BIO读取网络请求数据,阻塞,发生进程调度 request = connFd.read(); // BIO读取本地文件,阻塞,发生进程调度 content = ReadFile('test.txt'); // 将文件内容写入响应 Response.write(content); } }
Multi-threaded applications are actually similar to multi-processes, except that one request is allocated to one process instead of one request to one thread. Threads are lighter than processes and occupy less system resources. Context switching (ps: the so-called context switching, a little explanation: a single-core CPU can only execute tasks in one process or thread at the same time, but for the sake of macro Parallelism on the Internet requires switching back and forth between multiple processes or threads according to time slices to ensure that each process and thread has a chance to be executed.) The overhead is also smaller; at the same time, it is easier to share memory between threads, which facilitates development
The two core points of web applications are mentioned above, one is the thread (thread) model and the other is the I/O model. So what exactly is blocking I/O? What other I/O models are there? Don't worry, first let's take a look at what is blocking
In short, blocking means that before the function call returns, the current (thread) thread will be suspended and enter the waiting state. In this state, the current (thread) thread will pause. , causing the CPU's incoming (thread) process scheduling. The function will only return to the caller after all the internal work is completed
So blocking I/O means that after the application calls the I/O operation through the API, the current thread (thread) will enter the waiting state , the code cannot continue to execute. At this time, the CPU can perform thread (thread) scheduling, that is, switch to other executable threads to continue execution. After the current thread (thread) completes processing of the underlying I/O request Only then will it return and continue execution
After understanding what blocking and blocking I/O are, let’s analyze the disadvantages of the multi-thread (thread) blocking I/O model of traditional web applications.
Because a request needs to be allocated a thread (thread), such a system needs to maintain a large number of threads (threads) when the amount of concurrency is large, and a large number of context switches are required, which requires a large amount of CPU, System resources such as memory support, so when high concurrent requests come in, the CPU and memory overhead will rise sharply, which may quickly bring down the entire system and cause the service to be unavailable
Next let’s take a look at how nodejs applications are implemented.
const net = require('net'); const server = net.createServer(); const fs = require('fs'); server.listen(80); // 监听端口 // 监听事件建立连接 server.on('connection', (socket) => { // 监听事件读取请求数据 socket.on('data', (data) => { // 异步读取本地文件 fs.readFile('test.txt', (err, data) => { // 将读取的内容写入响应 socket.write(data); socket.end(); }) }); });
You can see that in nodejs, we can perform I/O operations in an asynchronous manner. After calling the I/O operation through the API It will return immediately, and then you can continue to execute other code logic. So why is I/O in nodejs "non-blocking"? Before answering this question, let’s do some preparation work. Refer to the nodejs advanced video explanation: Enter learning
Watch first What steps do the next read operation need to go through
接下来我们看一下操作系统中有哪些I/O模型
阻塞式I/O
非阻塞式I/O
I/O多路复用(进程可同时监听多个I/O设备就绪)
信号驱动I/O
异步I/O
那么nodejs里到底使用了哪种I/O模型呢?是上图中的“非阻塞I/O”吗?别着急,先接着往下看,我们来了解下nodejs的体系结构
最上面一层是就是我们编写nodejs应用代码时可以使用的API库,下面一层则是用来打通nodejs和它所依赖的底层库的一个中间层,比如实现让js代码可以调用底层的c代码库。来到最下面一层,可以看到前端同学熟悉的V8,还有其他一些底层依赖。注意,这里有一个叫libuv的库,它是干什么的呢?从图中也能看出,libuv帮助nodejs实现了底层的线程池、异步I/O等功能。libuv实际上是一个跨平台的c语言库,它在windows、linux等不同平台下会调用不同的实现。我这里主要分析linux下libuv的实现,因为我们的应用大部分时候还是运行在linux环境下的,且平台间的差异性并不会影响我们对nodejs原理的分析和理解。好了,对于nodejs在linux下的I/O模型来说,libuv实际上提供了两种不同场景下的不同实现,处理网络I/O主要由epoll函数实现(其实就是I/O多路复用,在前面的图中使用的是select函数来实现I/O多路复用,而epoll可以理解为select函数的升级版,这个暂时不做具体分析),而处理文件I/O则由多线程(线程池) + 阻塞I/O模拟异步I/O实现
下面是一段我写的nodejs底层实现的伪代码帮助大家理解
listenFd = new Socket(); // 创建监听socket Bind(listenFd, 80); // 绑定端口 Listen(listenFd); // 开始监听 for ( ; ; ) { // 阻塞在epoll函数上,等待网络数据准备好 // epoll可同时监听listenFd以及多个客户端连接上是否有数据准备就绪 // clients表示当前所有客户端连接,curFd表示epoll函数最终拿到的一个就绪的连接 curFd = Epoll(listenFd, clients); if (curFd === listenFd) { // 监听套接字收到新的客户端连接,创建套接字 int connFd = Accept(listenFd); // 将新建的连接添加到epoll监听的list clients.push(connFd); } else { // 某个客户端连接数据就绪,读取请求数据 request = curFd.read(); // 这里拿到请求数据后可以发出data事件进入nodejs的事件循环 ... } } // 读取本地文件时,libuv用多线程(线程池) + BIO模拟异步I/O ThreadPool.run((callback) => { // 在线程里用BIO读取文件 String content = Read('text.txt'); // 发出事件调用nodejs提供的callback });
通过I/O多路复用 + 多线程模拟的异步I/O配合事件循环机制,nodejs就实现了单线程处理并发请求并且不会阻塞。所以回到之前所说的“非阻塞I/O”模型,实际上nodejs并没有直接使用通常定义上的非阻塞I/O模型,而是I/O多路复用模型 + 多线程BIO。我认为“非阻塞I/O”其实更多是对nodejs编程人员来说的一种描述,从编码方式和代码执行顺序上来讲,nodejs的I/O调用的确是“非阻塞”的
至此我们应该可以了解到,nodejs的I/O模型其实主要是由I/O多路复用和多线程下的阻塞I/O两种方式一起组成的,而应对高并发请求时发挥作用的主要就是I/O多路复用。好了,那最后我们来总结一下nodejs线程模型和I/O模型对比传统web应用多进(线)程 + 阻塞I/O模型的优势和劣势
For more node-related knowledge, please visit: nodejs tutorial!
The above is the detailed content of A brief analysis of the principles of high concurrency in Node. For more information, please follow other related articles on the PHP Chinese website!