简体   繁体   English

NodeJS-它如何处理请求

[英]NodeJS - how does it handles requests

Suppose you have the code below: 假设您有以下代码:

var http = require('http'); 
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  //Computations that takes one millisecond
  ............
  res.end('Hello World\n');
}).listen(8124, "127.0.0.1");

Questions: 问题:

  1. Will Node handle that callbacks in parallel? Node是否会并行处理该回调?
  2. What would happen if we sent more that 1 request per millisecond (let's say 3 requests simultaneously)? 如果我们每毫秒发送超过1个请求(假设同时有3个请求),会发生什么情况?
  3. What would happen if we sent 150 000 requests per second? 如果我们每秒发送15万个请求,将会发生什么?

Though I am new to nodejs, I will try to give some advices. 尽管我是NodeJS的新手,但我将尝试提供一些建议。

IMHO, Node doesn't handle any parallelism. 恕我直言,Node不处理任何并行性。 It is single-thread, means only 1 process, 1 thread, 1 cpu. 它是单线程的,仅意味着1个进程,1个线程,1个cpu。 All his power resides in his non-blocking way of processing by using callback functions. 他的全部权力都在于使用回调函数的非阻塞处理方式。 This makes node stay rapid without the complexity of multithread. 这使节点保持快速运行,而没有多线程的复杂性。

If a single nodejs server is not enough for your huge incoming request, think of cluster to take advantage of multi-core system : http://nodejs.org/api/cluster.html 如果单个nodejs服务器不足以满足您的巨大传入请求,请考虑集群以利用多核系统: http : //nodejs.org/api/cluster.html

Just google. 只是谷歌。 There are lots of articles on this topic. 关于此主题的文章很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM