我爱编程

Node.js event loop

2018-06-10  本文已影响0人  SummerDreamEve

参考http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/

image.png

1.The first basic thesis of node.js is that I/O is expensive

synchronous:

you handle one request at a time, each in turn. pros: simple cons: any one request can hold up all the other requests

fork a new process

you start a new process to handle each request. pros: easy cons: does not scale well, hundreds of connections means hundreds of processes. fork() is the Unix programmer's hammer. Because it's available, every problem looks like a nail. It's usually overkill

threads

start a new thread to handle each request. pros: easy, and kinder to the kernel than using fork, since threads usually have much less overhead cons: your machine may not have threads, and threaded programming can get very complicated very fast, with worries about controlling access to shared resources.

2. The second basis thesis is that thread-per-connection is memory-expensive

Apache is multithreaded: it spawns a thread per request (or process, it depends on the conf). You can see how that overhead eats up memory as the number of concurrent connections increases and more threads are needed to serve multiple simulataneous clients.

上一篇 下一篇

猜你喜欢

热点阅读