简体   繁体   English

Node.js 多线程:什么是工作线程,它是如何工作的?

[英]Node.js multithreading: What are Worker threads and how does it work?

I always believed that JS was a single threaded language which makes it inefficient for CPU intensive tasks.我一直认为 JS 是一种单线程语言,这使得它对于 CPU 密集型任务效率低下。 I recently came across worker threads and how it solves this inefficiency problem by creating "multiple worker threads under one process".我最近遇到了工作线程,以及它如何通过创建“一个进程下的多个工作线程”来解决这个效率低下的问题。 What's the difference between a process and a thread?进程和线程有什么区别? Why is JS all of the sudden capable of spawning multiple worker threads that help and interact with the main JS thread to enable concurrency?为什么 JS 突然能够产生多个工作线程来帮助主 JS 线程并与之交互以实现并发? Could you help me understand this topic in layman terms?你能帮我用外行的方式理解这个话题吗? Thank you谢谢

Starting in node v10, they introduced WorkerThreads.从节点 v10 开始,他们引入了 WorkerThreads。 A WorkerThread is an entirely new instance of the V8 Javascript interpreter. WorkerThread 是 V8 Javascript 解释器的全新实例。 It has it's own set of variables, it's own globals and it's own thread of running Javascript.它有自己的变量集、全局变量和运行 Javascript 的线程。 You cannot directly share regular Javascript variables between the main thread and a workerThread or between workerThreads.您不能在主线程和 workerThread 之间或 workerThread 之间直接共享常规 Javascript 变量。

You can directly share memory if it is specifically allocated as SharedMemory such as a SharedArrayBuffer , but when doing so, you open yourself up to race conditions between the two threads both accessing the Shared memory.如果 memory 被专门分配为 SharedMemory(例如SharedArrayBuffer ),则可以直接共享它,但是这样做时,您将面临两个线程之间的竞争条件,这两个线程都访问共享 memory。 So, you have to either use Atomics or your own concurrency management scheme to prevent race conditions when modifying the shared memory.因此,在修改共享 memory 时,您必须使用 Atomics 或您自己的并发管理方案来防止竞争条件。

The main thread and workerThreads can send messages to each other and those messages can contain some types of data structures that will be copied via a structured cloning mechanism and sent to the other V8 instance.主线程和工作线程可以相互发送消息,这些消息可以包含某些类型的数据结构,这些数据结构将通过结构化克隆机制复制并发送到另一个 V8 实例。

The idea behind workerThreads is that they are useful for getting CPU-intensive code out of your main event loop (particularly useful for servers) so you can fire up one or more workerThreads to handle CPU-intensive work and keep the main thead event loop free and responsive to incoming events/networking/etc... workerThreads 背后的想法是,它们有助于将 CPU 密集型代码从主事件循环中取出(对服务器特别有用),因此您可以启动一个或多个 workerThreads 来处理 CPU 密集型工作并保持主线程事件循环空闲并响应传入事件/网络/等...

You can also do something similar by creating multiple nodejs processes.你也可以通过创建多个 nodejs 进程来做类似的事情。 But, a process is a heavier-weight thing than a workerThread and workerThreads allow you to share memory with SharedMemory whereas separate processes do not.但是,进程比 workerThread 更重,workerThreads 允许您与 SharedMemory 共享 memory,而单独的进程则不能。

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

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