简体   繁体   English

Node.js是单线程的,但如何在1秒内处理相同的并发请求

[英]Nodejs is single threaded but how it can process same concurrent request in 1 sec

I have scenerio Assume a client send a request to node server and node js take 1sec to process it with single thread . 我有scenerio假设客户端向节点服务器发送请求,而节点js用单线程处理它需要1秒。 Then my question is : 那么我的问题是:

How much time it will take to process same 1000 request come at same moment. 处理同一1000个请求将花费多少时间在同一时间。

I know the answer which is only 1sec . 我知道答案只有1秒。 But i dont know how. 但是我不知道如何。 It should be take 1000sec because each Request is taking 1sec for processing. 应该花费1000秒,因为每个请求都需要1秒来处理。 . Please tell me how is it possible to complete all request in only one second by singlethread 请告诉我如何通过单线程在一秒钟内完成所有请求

If the actual full CPU usage is 1 second per request, then 1000 requests will take 1000 seconds to process. 如果每个请求的实际完整CPU使用率是1秒,那么处理1000个请求将花费1000秒。 There is no magic bullet around that. 周围没有魔术子弹。 So, to process 1000/sec, you would need 1000 CPUs (obviously spread across a number of clustered servers). 因此,要以每秒1000个的速度处理,您将需要1000个CPU(显然分布在许多群集服务器上)。

But, if the 1 second response time is actually just the total time of the request and much of the time node.js may be waiting for a database lookup or a file operation (both of which are asynchronous), then the CPU is sitting idle much of the time and while one request is waiting for some I/O operation to respond, it can be working on another request. 但是,如果1秒钟的响应时间实际上只是请求的总时间,而node.js可能大部分时间在等待数据库查找或文件操作(两者都是异步的),则CPU处于空闲状态在很多时候,一个请求正在等待某些I / O操作响应时,它可能正在处理另一个请求。 And, then when that one is waiting for I/O, it can start up another request. 然后,当那个人在等待I / O时,它可以启动另一个请求。 In that way, a single thread can have many different requests that are in-flight at the same time (assuming these requests are doing some async I/O as part of their processing). 这样,单个线程可以同时具有多个进行中的不同请求(假设这些请求在处理过程中正在执行一些异步I / O)。 In this way, node.js interleaves multiple different operations, but all with one single thread. 通过这种方式,node.js交错执行多个不同的操作,但是所有操作都只有一个线程。

This is the big scale advantage of the design of node.js. 这是node.js设计的大规模优势。 Rather than needing a fairly heavyweight thread for each concurrent request, it can actually be servicing a number of requests with a single thread. 与其为每个并发请求都需要一个相当重的线程,它实际上可以通过一个线程为多个请求提供服务。 Instead of using OS-level scheduling and multiple stack frames to time slice OS-level threads, node.js just yields control to another event in the event queue whenever one piece of JS returns control back to the system because it is waiting for an I/O response event to come back. 无需使用操作系统级调度和多个堆栈帧来划分操作系统级线程的时间,而是只要一个JS块正在等待控制返回给系统,Node.js便将控制权交给事件队列中的另一个事件。 / O响应事件回来。

Please tell me how is it possible to complete all request in only one second by singlethread 请告诉我如何通过单线程在一秒钟内完成所有请求

This would only be possible if the actual CPU time for any given request was less than 1/1000th of a second. 仅在任何给定请求的实际CPU时间少于1/1000秒的情况下才有可能。 Otherwise, you will have to involve more than one CPU in order to process 1000 requests/sec. 否则,您将必须使用多个CPU才能处理1000个请求/秒。 You may also need more than one network card too because you're talking about being able to read a request and send a response all in less than 1ms. 您可能还需要多个网卡,因为您正在谈论的是能够在不到1ms的时间内读取请求并发送响应。 Not likely with a single network card. 单个网卡不太可能。

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

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