How the single threaded non blocking IO model works in Node.js

后端 未结 7 976
抹茶落季
抹茶落季 2020-11-22 05:29

I\'m not a Node programmer, but I\'m interested in how the single threaded non blocking IO model works. After I read the article understanding-the-node-js-e

7条回答
  •  余生分开走
    2020-11-22 06:15

    Well, to give some perspective, let me compare node.js with apache.

    Apache is a multi-threaded HTTP server, for each and every request that the server receives, it creates a separate thread which handles that request.

    Node.js on the other hand is event driven, handling all requests asynchronously from single thread.

    When A and B are received on apache, two threads are created which handle requests. Each handling the query separately, each waiting for the query results before serving the page. The page is only served until the query is finished. The query fetch is blocking because the server cannot execute the rest of thread until it receives the result.

    In node, c.query is handled asynchronously, which means while c.query fetches the results for A, it jumps to handle c.query for B, and when the results arrive for A arrive it sends back the results to callback which sends the response. Node.js knows to execute callback when fetch finishes.

    In my opinion, because it's a single thread model, there is no way to switch from one request to another.

    Actually the node server does exactly that for you all the time. To make switches, (the asynchronous behavior) most functions that you would use will have callbacks.

    Edit

    The SQL query is taken from mysql library. It implements callback style as well as event emitter to queue SQL requests. It does not execute them asynchronously, that is done by the internal libuv threads that provide the abstraction of non-blocking I/O. The following steps happen for making a query :

    1. Open a connection to db, connection itself can be made asynchronously.
    2. Once db is connected, query is passed on to the server. Queries can be queued.
    3. The main event loop gets notified of the completion with callback or event.
    4. Main loop executes your callback/eventhandler.

    The incoming requests to http server are handled in the similar fashion. The internal thread architecture is something like this:

    node.js event loop

    The C++ threads are the libuv ones which do the asynchronous I/O (disk or network). The main event loop continues to execute after the dispatching the request to thread pool. It can accept more requests as it does not wait or sleep. SQL queries/HTTP requests/file system reads all happen this way.

提交回复
热议问题