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

后端 未结 7 968
抹茶落季
抹茶落季 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:04

    if you read a bit further - "Of course, on the backend, there are threads and processes for DB access and process execution. However, these are not explicitly exposed to your code, so you can’t worry about them other than by knowing that I/O interactions e.g. with the database, or with other processes will be asynchronous from the perspective of each request since the results from those threads are returned via the event loop to your code."

    about - "everything runs in parallel except your code" - your code is executed synchronously, whenever you invoke an asynchronous operation such as waiting for IO, the event loop handles everything and invokes the callback. it just not something you have to think about.

    in your example: there are two requests A (comes first) and B. you execute request A, your code continue to run synchronously and execute request B. the event loop handles request A, when it finishes it invokes the callback of request A with the result, same goes to request B.

提交回复
热议问题