NodeJS 中的分叉

发布于 2024-12-12 06:23:39 字数 852 浏览 0 评论 0原文

我对如何在 NodeJS 中创建守护进程有点困惑

在调用 fork() 之前,我已经在 C 中创建了守护进程,它们从子进程中进行调用的位置继续执行,从而允许父终止。我无法使用 process.fork() 和 process.kill() 轻松实现相同的效果。

以下代码没有达到我的预期并且中断:

var current_pid, cp = require('child_process');
current_pid = process.pid;
cp.fork('');
process.kill(current_pid);

发出以下错误,我无法弄清楚为什么或发生了什么:

node.js:202
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: read EBADF
    at errnoException (net.js:589:11)
    at Pipe.onread (net.js:335:20)

问题调用似乎是 process.kill()。删除这个,两个进程都会继续愉快地运行。

我知道 daemon.node,但它是在 child_process.fork()< 时创建的/code> 不存在(v0.1.33 是编写 daemon.node 时可用的版本)。现在有一种原生的分叉方式,所以这应该不再是必要的。 (另外,它似乎也被放弃了。)

I'm a little bit confused as to how to create daemons in NodeJS

I've created daemons in C before that call fork() that continue execution from where the call was made in a child process allowing the parent to terminate. I can't readily achieve the same affect using process.fork() and process.kill().

The following code doesn't do what I expected and breaks:

var current_pid, cp = require('child_process');
current_pid = process.pid;
cp.fork('');
process.kill(current_pid);

The following error is emitted and I can't work out why or what's happening:

node.js:202
        throw e; // process.nextTick error, or 'error' event on first tick
              ^
Error: read EBADF
    at errnoException (net.js:589:11)
    at Pipe.onread (net.js:335:20)

The problem call appears to be process.kill(). Removing this, both processes continue to happily run.

I'm aware of daemon.node, but this was created at the time when child_process.fork() didn't exist (v0.1.33 was the version available when daemon.node was written). Now there is a native way to fork, so this should no longer be necessary. (Plus, it appears to have been abandoned too.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

child_process.fork() 有一个完全误导性的名称,并且与 C 的 fork() 不同。

根据文档,它执行 Node.js 脚本作为子进程并在调用进程和子进程之间建立通信通道。就是这样。

子进程的实际生成在libuv内部完成,Node 的 C 语言“平台层”,以及 fork() 本身并不暴露给 Node 脚本。

一种仅使用 Node.js 内置内容进行守护进程的简单且可大量改进的方法可能如下所示:

if (process.argv[2] !== 'child') {
    require('child_process').execFile(process.argv[0], [__filename, 'child']);
    process.exit();
}

setTimeout(function(){
    console.log('foo');
}, 5000);

显然,这与 fork() 非常不同。如果 daemon.node 适合您,请继续使用它。

child_process.fork() has a totally misleading name and is not the same as C’s fork().

According to the docs, it executes a Node.js script as a child process and sets up a communications channel between the calling process and the child. That's it.

The actual spawning of the child process is done inside libuv, Node's “platform layer,” in C, and fork() itself is not exposed to Node scripts.

A simple, much-improvable way to daemonize using only what’s built-in to Node.js might look like this:

if (process.argv[2] !== 'child') {
    require('child_process').execFile(process.argv[0], [__filename, 'child']);
    process.exit();
}

setTimeout(function(){
    console.log('foo');
}, 5000);

Obviously, this is pretty different from fork(). If daemon.node works for you, keep using it.

帅气尐潴 2024-12-19 06:23:39

daemon.node 继续在 https://github.com/indexzero/daemon.node 进行开发。

daemon.node continues to be developed at https://github.com/indexzero/daemon.node.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文