Luc*_*eis 5 javascript node.js
在不同进程中运行某些代码并将其结果与主进程通信的最简单方法是什么?
所以我有一个非常密集的任务,需要分成不同的流程.做这样的事最简单的方法是什么?
// in main process
var otherProcess = createAnotherProcess(function() {
console.log("this code is ran in another process");
return "some data";
});
otherProcess.on("done", function(data) {
console.log(data); // will output "some data"
});
Run Code Online (Sandbox Code Playgroud)
拥有能够在多个进程中运行代码的单个源代码文件将是惊人的!这甚至可能吗?我已尝试在节点中阅读一些关于"child_processes"的内容,但发现它有点过于复杂.
有帮助吗?
var spawn = require("child_process").spawn;
var stat = spawn("dstat", ["-r", "--noheaders", "--nocolor"]);
var output = function (output_data) {
//do something with output_data
};
stat.stdout.on("data", output);
Run Code Online (Sandbox Code Playgroud)
http://nodejs.org/docs/v0.4.11/api/child_processes.html