Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在node.js中分叉self?_Node.js - Fatal编程技术网

如何在node.js中分叉self?

如何在node.js中分叉self?,node.js,Node.js,我想在nodejs app.js的一开始就创建一个新流程,然后在主流程中创建一个http服务器,在新流程中,我将运行while true循环来完成一些工作 我读过child\u process.fork(modulePath,…),它需要一个modulePath,在我的情况下,我只需要在C 大概是这样的: // very begin of the server.js fork(function(new_process) { // child process while(true) {

我想在nodejs app.js的一开始就创建一个新流程,然后在主流程中创建一个http服务器,在新流程中,我将运行
while true
循环来完成一些工作

我读过
child\u process.fork(modulePath,…)
,它需要一个
modulePath
,在我的情况下,我只需要在
C

大概是这样的:

// very begin of the server.js
fork(function(new_process) {
  // child process
  while(true) {
    //do sth
  }
});
// main process
// balabala
server.listen(3000, function () {
  console.log('Express server listening on port 3000');
});
正如您所发现的,“fork”的名称很糟糕:它实际上是一个fork/exec

你可能想考虑以下任何一个选项:

您也可以考虑NoDE.JS集群:

最后,本文可能会引起兴趣: