在 Node.js 中并行化任务 [英] Parallelizing tasks in Node.js

查看:34
本文介绍了在 Node.js 中并行化任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 JS 中完成一些资源密集型任务.对于这个问题,让我们假设它们是一些繁重的计算,而不是系统访问.现在我想同时运行任务 A、B 和 C,并在完成后执行一些函数 D.

I have some tasks I want to do in JS that are resource intensive. For this question, lets assume they are some heavy calculations, rather then system access. Now I want to run tasks A, B and C at the same time, and executing some function D when this is done.

异步库为此提供了一个很好的脚手架:

The async library provides a nice scaffolding for this:

async.parallel([A, B, C], D);

如果我正在做的只是计算,那么这仍然会同步运行(除非库将任务放在不同的线程上,我希望不是这种情况).我如何使这实际上是平行的?异步代码通常会做什么来不阻塞调用者(使用 NodeJS 时)?是否启动了子进程?

If what I am doing is just calculations, then this will still run synchronously (unless the library is putting the tasks on different threads itself, which I expect is not the case). How do I make this be actually parallel? What is the thing done typically by async code to not block the caller (when working with NodeJS)? Is it starting a child process?

推荐答案

我如何使这实际上是并行的?

首先,在单节点应用程序中,您不会真正并行运行.节点应用程序运行在单个线程上,并且节点的事件循环一次只处理一个事件.即使在多核机器上运行时,您也不会在节点应用程序中获得并行处理.

First, you won't really be running in parallel while in a single node application. A node application runs on a single thread and only one event at a time is processed by node's event loop. Even when running on a multi-core box you won't get parallelism of processing within a node application.

也就是说,您可以在多核机器上获得处理并行性通过将代码分叉到单独的节点进程中通过生成子进程.这实际上允许您创建节点本身的多个实例并以不同方式与这些进程通信(例如 stdout、进程分支 IPC 机制).此外,您可以选择将函数(按职责)分离到它们自己的节点应用程序/服务器中,并通过 RPC 调用它.

That said, you can get processing parallelism on multicore machine via forking the code into separate node processes or by spawning child process. This, in effect, allows you to create multiple instances of node itself and to communicate with those processes in different ways (e.g. stdout, process fork IPC mechanism). Additionally, you could choose to separate the functions (by responsibility) into their own node app/server and call it via RPC.

async 代码通常会做什么来不阻塞调用者(使用 NodeJS 时)? 是否正在启动子进程?

What is the thing done typically by async code to not block the caller (when working with NodeJS)? Is it starting a child process?

它不是开始一个新进程.下面,当在 node.js 中使用 async.parallel 时,它使用process.nextTick().nextTick() 允许您通过将工作推迟到新堆栈上来避免阻塞调用者,这样您就可以交错执行 CPU 密集型任务等.

It is not starting a new process. Underneath, when async.parallel is used in node.js, it is using process.nextTick(). And nextTick() allows you to avoid blocking the caller by deferring work onto a new stack so you can interleave cpu intensive tasks, etc.

长话短说

Node 并不能轻松实现开箱即用"的多处理器并发.相反,Node 为您提供了一个非阻塞设计和一个事件循环,该循环利用一个线程而不共享内存.多个线程不能共享数据/内存,因此不需要锁.节点无锁.一个节点进程利用一个线程,这使得节点既安全又强大.

Node doesn't make it easy "out of the box" to achieve multiprocessor concurrency. Node instead gives you a non-blocking design and an event loop that leverages a thread without sharing memory. Multiple threads cannot share data/memory, therefore locks aren't needed. Node is lock free. One node process leverages one thread, and this makes node both safe and powerful.

当您需要在多个进程之间拆分工作时,请使用某种消息传递与其他进程/服务器进行通信.例如IPC/RPC.

更多见:

SO 关于 什么是 Node.js 的精彩回答...有很多好处.

Awesome answer from SO on What is Node.js... with tons of goodness.

了解 process.nextTick()

这篇关于在 Node.js 中并行化任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆