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

查看:200
本文介绍了在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);

如果我在做什么只是计算,那么这仍然会同步运行(除非该库是把任务在不同的线程本身,我希望不是这样)。如何让我这个被实际水货?什么是异步code通常要做的事情(有工作的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.

这是说,你就可以用分叉的code到单独的节点进程处理多核机器并行或<一个href=\"http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options\">by产卵子进程。这实际上,允许你创建节点自身的多个实例,并以不同方式与这些进程(例如标准输出,进程fork 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.

什么是通常要做的事情的异步的code(带的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?

这不是开始一个新的进程。下边,当async.parallel中的node.js 的使用,它是使用 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 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。

When you need to split work up among multiple processes then use some sort of message passing to communicate with the other processes / servers. e.g. IPC/RPC.

有关更多查看:

这等什么是Node.js的 ...以吨善良。

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

理解process.nextTick()

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

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