Referring to the following discussion: How is Node.js inherently faster when it still relies on Threads internally?
参考下面的讨论:当Node.js内部依赖Threads时,它本身如何更快?
After having gone through all responses, I still have basic questions: If a DB call is made, 'somebody' has to block for the call to return. It turns into a blocking call deep down. Somebody has to make a call to the DB. The 'somebody' has to be a thread. If there are 50 DB calls, though they appear to be non-blocking to the Javascript, deep down they have all blocked. If there are 50 calls, for them to be all fired together on the DB, they have to be each sent to the DB by a thread. This means there would be 50 threads that have sent the DB call and are waiting for their call to return. This is no different than having 50 threads like they do in Apache. Please rectify my understanding. What is Node.js doing cleverly and how to ensure that fewer threads than 50 run in this case?
在完成所有回复之后,我仍然有一些基本问题:如果进行数据库调用,“某人”必须阻止该回调。它变成了内心深处的阻塞。有人必须打电话给DB。 '某人'必须是一个线程。如果有50个DB调用,虽然它们似乎对Javascript没有阻塞,但在内心深处它们都被阻止了。如果有50个调用,为了将它们全部一起发送到DB上,它们必须通过一个线程分别发送给DB。这意味着将有50个线程已发送数据库调用并正在等待其调用返回。这与拥有50个线程没有什么不同,就像在Apache中一样。请纠正我的理解。什么是Node.js巧妙地做,以及如何确保在这种情况下运行的线程少于50?
2 个解决方案
#1
You are... partially correct. If there are 50 concurrent DB calls, then that means 50 threads, each dedicated to a DB call (actually, the reality is that by default, node provides only 4 concurrent threads in its thread pool, if you want more you have to explicitly specify how many threads you're willing to allow node to spin up; see my answer here - any excess requests are queued).
你是......部分正确。如果有50个并发数据库调用,则表示50个线程,每个线程专用于数据库调用(实际上,实际情况是,默认情况下,节点在其线程池中仅提供4个并发线程,如果您需要更多,则必须明确指定您愿意允许节点启动多少个线程;请参阅我的答案 - 任何多余的请求都排队等待。
What makes this more efficient than Apache is that each of those threads is dedicated to the smallest functional unit... it lives only for the life of that database call, and then it's relinquished (in this case, a new thread is created, up to the limit, and then that thread is put back into the pool). This is in dramatic opposition to Apache, which spins up a thread for each new request, and may have to service multiple database calls and other processing in between until that request is completed and can then be relinquished.
是什么让它比Apache更有效率是每个线程都专用于最小的功能单元...它只在该数据库调用的生命周期中存在,然后它被放弃(在这种情况下,创建一个新的线程,向上)到极限,然后该线程被放回池中)。这与Apache形成了极大的对立,它为每个新请求分配了一个线程,并且可能必须服务多个数据库调用和其他处理,直到该请求完成,然后可以放弃。
Ultimately, this results in each thread spending more of its time doing work or in the pool waiting for more work and less time being idle and unavailable.
最终,这会导致每个线程花费更多的时间在工作中或在池中等待更多的工作,更少的时间闲置和不可用。
Be aware that this is workload-dependent, there are workloads that work better in the Apache model, but in general, most web style workloads are more suited to the node model.
请注意,这与工作负载有关,在Apache模型中有一些工作负载可以更好地工作,但一般来说,大多数Web样式的工作负载更适合于节点模型。
#2
The difference is that I believe in something like apache, those threads are going to be handled in the order they are received. So if thread one is waiting for 10TB of data and thread two is only waiting for 10KB of data, thread two has to wait until thread one is done even though it's work could be done far faster and return quicker. With node the idea is that each thread waiting for I/O is returned as soon as it is done. So depending on the I/O the same situation could allow thread two to return before thread one is done it's work. This is older but still an excellent write up I believe of threading in node. http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
不同之处在于我相信像apache这样的东西,这些线程将按接收顺序处理。因此,如果线程1正在等待10TB数据而线程2只等待10KB数据,则线程2必须等到线程1完成,即使它的工作可以更快地完成并返回更快。对于节点,想法是每个等待I / O的线程在完成后立即返回。因此,根据I / O,相同的情况可能允许线程2在线程1完成之前返回它的工作。这是较旧但仍然是一个很好的写作我相信在节点中的线程。 http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
#1
You are... partially correct. If there are 50 concurrent DB calls, then that means 50 threads, each dedicated to a DB call (actually, the reality is that by default, node provides only 4 concurrent threads in its thread pool, if you want more you have to explicitly specify how many threads you're willing to allow node to spin up; see my answer here - any excess requests are queued).
你是......部分正确。如果有50个并发数据库调用,则表示50个线程,每个线程专用于数据库调用(实际上,实际情况是,默认情况下,节点在其线程池中仅提供4个并发线程,如果您需要更多,则必须明确指定您愿意允许节点启动多少个线程;请参阅我的答案 - 任何多余的请求都排队等待。
What makes this more efficient than Apache is that each of those threads is dedicated to the smallest functional unit... it lives only for the life of that database call, and then it's relinquished (in this case, a new thread is created, up to the limit, and then that thread is put back into the pool). This is in dramatic opposition to Apache, which spins up a thread for each new request, and may have to service multiple database calls and other processing in between until that request is completed and can then be relinquished.
是什么让它比Apache更有效率是每个线程都专用于最小的功能单元...它只在该数据库调用的生命周期中存在,然后它被放弃(在这种情况下,创建一个新的线程,向上)到极限,然后该线程被放回池中)。这与Apache形成了极大的对立,它为每个新请求分配了一个线程,并且可能必须服务多个数据库调用和其他处理,直到该请求完成,然后可以放弃。
Ultimately, this results in each thread spending more of its time doing work or in the pool waiting for more work and less time being idle and unavailable.
最终,这会导致每个线程花费更多的时间在工作中或在池中等待更多的工作,更少的时间闲置和不可用。
Be aware that this is workload-dependent, there are workloads that work better in the Apache model, but in general, most web style workloads are more suited to the node model.
请注意,这与工作负载有关,在Apache模型中有一些工作负载可以更好地工作,但一般来说,大多数Web样式的工作负载更适合于节点模型。
#2
The difference is that I believe in something like apache, those threads are going to be handled in the order they are received. So if thread one is waiting for 10TB of data and thread two is only waiting for 10KB of data, thread two has to wait until thread one is done even though it's work could be done far faster and return quicker. With node the idea is that each thread waiting for I/O is returned as soon as it is done. So depending on the I/O the same situation could allow thread two to return before thread one is done it's work. This is older but still an excellent write up I believe of threading in node. http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
不同之处在于我相信像apache这样的东西,这些线程将按接收顺序处理。因此,如果线程1正在等待10TB数据而线程2只等待10KB数据,则线程2必须等到线程1完成,即使它的工作可以更快地完成并返回更快。对于节点,想法是每个等待I / O的线程在完成后立即返回。因此,根据I / O,相同的情况可能允许线程2在线程1完成之前返回它的工作。这是较旧但仍然是一个很好的写作我相信在节点中的线程。 http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/