¿Cómo maneja NodeJs tantas solicitudes entrantes, utiliza un grupo de subprocesos?

When a request comes into a nodejs server, how does it handle the request?

I understand it has a different way of handling requests, as it doesn't spawn a new thread for each request (or I guess it doesn't use a traditional thread pool either).

Can someone explain to me what is going on under the hood, and does the flavour of linux matter here?

preguntado el 08 de noviembre de 11 a las 19:11

2 Respuestas

No, it does async IO. There's only one thread that blocks until something happens somewhere, and then it handles that event. This means that one thread in one process can serve many concurrent connections. Somewhat like

endless loop {
  event = next_event()
  dispatch_event(event)
}

The only exception is filesystem stuff, it uses a thread pool for that under the hood.

respondido 08 nov., 11:23

so you mean it is actually serialized? when a request is 'sleeping', how does it wake up and tell the thread to start? - completar código

@codecompleting: You can tell the OS to notify you when data comes in on a socket or so. - thejh

Would there be any advantage to making NodeJS multi-threaded? Or can it basically fully utilize a CPU with one thread? And can NodeJS use multiple cores? - Mike Christensen

@MikeChristensen: You can make multiple processes with node and use all cores with that, but node isn't really made for CPU-intensive stuff. If you need multi-process webservers or so, you can use the builtin "cluster" feature, it lets you run as many copies of your program as you want to and it'll pass incoming connections to random processes. - thejh

How mamy events can node ja store? Where do they sit? How to measure their count? - meso_2600

Node tells the operating system (through epoll, kqueue, /dev/polly select) that it should be notified when a new connection is made, and then it goes to sleep. If someone new connects, then it executes the callback. Each connection is only a small heap allocation

It is "event driven" where it handles IO in an async fashion (non blocking I/O). It internally does threading needed to do epoll, kqueue, /dev/polly select handling, but for you as a user/client it is absolutely transparent.

p.ej epoll is not really a thread pool, but an OS' I/O event notification facility, that node.js sits on top of.

respondido 08 nov., 11:23

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.