Grasping the Node JS alternative to multithreading

后端 未结 5 1110
慢半拍i
慢半拍i 2020-11-28 18:58

If I understand correctly Node JS is non blocking...so instead of waiting for a response from a database or other process it moved on to something else and checks back later

5条回答
  •  猫巷女王i
    2020-11-28 19:58

    Pretty much correct, yes. The node.js server has an internal thread pool so it can perform blocking operations and notify the main thread with a callback or event when things complete.

    So I imagine that it will make limited use of another core for the thread pool, for example if you do a non-blocking file system read this is likely implemented by telling a thread from the thread pool to perform a read and set a callback when it's done which means that the read could be happening on a different thread/core while the main node.js program is doing something else.

    But from a node.js point of view, it's entirely single threaded and won't directly use more than one core.

提交回复
热议问题