在基于PHP(或Java/ASP.NET/Ruby)的web服务器中,每个客户端请求都在一个新线程上实例化。但是在Node.js中,所有的客户端都运行在同一个线程上(它们甚至可以共享相同的变量!)我知道I/O操作是基于事件的,所以它们不会阻塞主线程循环。
What I don't understand is WHY the author of Node chose it to be single-threaded? It makes things difficult. For example, I can't run a CPU intensive function because it blocks the main thread (and new client requests are blocked) so I need to spawn a process (which means I need to create a separate JavaScript file and execute another node process on it). However, in PHP cpu intensive tasks do not block other clients because as I mentioned each client is on a different thread. What are its advantages compared to multi-threaded web servers?
注意:我已经使用集群来解决这个问题,但它并不漂亮。