简体   繁体   English

NodeJS中的工人是什么

[英]What are workers in NodeJS

I've primarily programmed in other programming languages but I have been making a webapp in user NodeJS and have come across a few things that I can't quite get my heard around. 我主要使用其他编程语言进行编程,但是我一直在用NodeJS用户制作一个Web应用程序,并且遇到了一些我听不清楚的东西。

I referred to https://nodejs.org/api/cluster.html#cluster_how_it_works 我提到了https://nodejs.org/api/cluster.html#cluster_how_it_works

I found that this explained, well, how NodeJS can cope with large numbers of requests despite Node only being single threaded. 我发现这很好地解释了,尽管Node仅是单线程的,但NodeJS如何处理大量请求。 However, what confuses me is when it says a port is shared among 'many workers'. 但是,令我感到困惑的是,当它说“许多工人”之间共享一个港口时。

Now if Node is not multithreaded then what exactly are these workers. 现在,如果Node不是多线程的,那么这些工作程序到底是什么。 In java for example you can have multithreaded applications using Completable Futures for example. 例如,在java中,您可以使用例如Completable Futures的多线程应用程序。 These cause different threads to take responsibility. 这些导致不同的线程承担责任。

But what is a worker in node if not a thread? 但是,如果不是线程,节点中的worker是什么?

Node can easily handle 10,000 concurrent connections in a single thread (see this answer for details). Node可以在单个线程中轻松处理10,000个并发连接(有关详细信息,请参见此答案 )。 For some things that are blocking it uses a thread pool but this is transparent to you. 对于某些阻塞的事情,它使用线程池,但这对您是透明的。 Your JavaScript uses a single-threaded event loop in every process. 您的JavaScript在每个进程中都使用单线程事件循环。

Keep in mind that nginx, a web server that is known for speed is also single-threaded. 请记住,以速度闻名的Web服务器nginx也是单线程的。 Redis, a database that is known for speed is also single-threaded. Redis是一个以速度闻名的数据库,它也是单线程的。 Multi-threading is good for CPU-bound tasks (when you use one thread per CPU) but for I/O-bound tasks that Node is usually used for, single-threaded event loops work better. 多线程适用于CPU绑定的任务(当每个CPU使用一个线程时),但是对于通常用于Node的I / O绑定任务,单线程事件循环的效果更好。

Now, to answer your question - in the context of clusters that the website that you linked to is talking about, a worker is a single process. 现在,要回答您的问题-在您链接到的网站所谈论的集群的背景下,工作人员是一个单一的过程。 Every one of those processes still has one single-threaded event loop but there can be many of those processes executing at the same time. 这些进程中的每个进程都仍然具有一个单线程事件循环,但是可能有许多进程同时执行。

See those answers for more details: 查看这些答案以获取更多详细信息:

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

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