Node.js后台任务

我有一个应用程序,其中有一部分是计算密集型的。 我想将这个计算closures到一个后台任务,并在完成时通知主Node.js线程。 我看到了许多可能的解决scheme,包括节点后台任务 ,ClusterHub, 节点线程A-Go-Go ,我相信还有其他的解决scheme。

有没有人有这方面的经验,可以推荐一个解决scheme?

谢谢,约翰

进程非常简单,除非需要特殊的function,比如自动重新构build后台守护进程,否则用一个库或npm模块来包装它们是不值得的。 内置的Node API“ child_process ”处理进程,使用它很简单:

var childProcess = require("child_process"); var child = childProcess.spawn("/usr/bin/uname", ["-a"]); child.stdout.setEncoding("utf8"); child.stderr.setEncoding("utf8"); var stdoutBuffer = "", stderrBuffer = ""; child.stdout.on("data", function(data){stdoutBuffer += data;}); child.stderr.on("data", function(data){stderrBuffer += data;}); child.on("close", function(code) { //the process exited with code `code` console.log("Stdout:", stdoutBuffer); process.exit(code); //bubble up the exit code }); 

如果您需要与订单无关的asynchronous控制stream,请考虑使用promise(我推荐kriskowal使用npm模块' q '。)在任务启动时创build一个promise,并在完成时parsing/拒绝它(在“close”事件处理程序中)在完成后台工作之后,将做出一个干净而可预测的方式来执行后续任务。