简体   繁体   English

如何以与Linux系统调用的fork()相同的方式分叉/克隆相同的Node子进程?

[英]How to fork/clone an identical Node child process in the same sense as fork() of Linux system call?

So I was developing a server farm on Node which requires multiple processes per machine to handle the load. 所以我在Node上开发一个服务器场,每个机器需要多个进程来处理负载。 Since Windows doesn't quite get along with Node cluster module, I had to manually work it out. 由于Windows与Node cluster模块不太相配,我不得不手动完成它。

The real problem is when I was forking Node processes, a JS module path was required as the first argument to the child_process.fork() function and once forked, the child process wouldn't inherit anything from its parent. 真正的问题是当我分配节点进程时,需要一个JS模块路径作为child_process.fork()函数的第一个参数,并且一旦分叉,子进程就不会从其父进程继承任何东西。 In my case, I want a function that does similar thing as fork() system call in Linux, which clones the parent process, inherits everything and continues execution from exactly where the fork() is done. 在我的例子中,我想要一个与Linux中的fork()系统调用类似的函数,该函数克隆父进程,继承所有内容并从fork()的确切位置继续执行。 Can this be achieved on the Node platform? 这可以在Node平台上实现吗?

I don't think node.js is ever going to support fork(2) 我不认为node.js会支持fork(2)

The comment from the node github page on the subject 关于该主题的节点github页面的注释

https://github.com/joyent/node/issues/2334#issuecomment-3153822 https://github.com/joyent/node/issues/2334#issuecomment-3153822

We're not (ever) going to support fork. 我们不会(永远)支持fork。

not portable to windows 不能移植到Windows

difficult conceptually for users 用户在概念上很难

entire heap will be quickly copied with a compacting VM; 使用压缩VM快速复制整个堆; no benefits from copy-on-write 没有写入时复制的好处

not necessary 不必要

difficult for us to do 我们很难做到

child_process.fork() child_process.fork()

This is a special case of the spawn() functionality for spawning Node processes. 这是产生Node进程的spawn()功能的特例。 In addition to having all the methods in a normal ChildProcess instance, the returned object has a communication channel built-in. 除了在普通的ChildProcess实例中具有所有方法之外,返回的对象还具有内置的通信通道。 See child.send(message, [sendHandle]) for details. 有关详细信息,请参阅child.send(message,[sendHandle])。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM