What does it mean to say Apache spawns a thread per request, but node.js does not?

前端 未结 3 1347
眼角桃花
眼角桃花 2020-12-01 08:41

I have read about node.js and other servers such as Apache, where the threading is different. I simply do not understand what the threading means.

If I have a webpa

3条回答
  •  孤街浪徒
    2020-12-01 09:09

    What people confuse between is Threads, Process & Async, Non-blocking I/O.

    Threads are child level 'runnable' to a process. All the execution environment is set up for a thread. Right from the Stack to Addressable memory locations it's allocated to a thread. If a child-level thread has to communicate back to the the main process thread, it has to use safe-messaging,notification models. There are multiple ways to do this, based on the language.

    Node.js is Single Threaded and obviously single Process based. It's not meant for high CPU intensive blocking calls. But if you still want to use, You could consider Node clustering. So instead of creating threads, it creates multiple "process" that works like a thread.

    Async - All the code that carries a callback functions are not actually Async. Okay in other words, Literally, they are Asynchrounous as they don't block the call.

    But in Node.js context, When someone says, Node is Async, it's completely linked to the OS interfacing. The capability of Node depends on the Non-blocking I/O capabilities of the underlying OS. So whatever objects the OS supports Non-blocking I/O for example, Sockets, Files, Pipes, Node utilizes them to maximum.

    And btw, when you talk about Apache, you should ideally be comparing Nginx. Not Node.js. Node.js is not meant to serve as a Web Server. It's a basically a Process that puts effective use of Async I/O.

提交回复
热议问题