javascript - 仅在 Nodejs 中需要时才从主进程启动和停止子进程

标签 javascript node.js process.start kill-process child-process

我对 Nodejs 和子进程有点陌生,现在在我的应用程序中遇到了一些麻烦。关于背景的一些信息:我有一个 nodejs-Websocket-Server 正在工作,它可以向任何正在连接的用户加载 HTML 页面。如果用户按下 HTML 上的按钮,它会向服务器发送一条消息,指示服务器应该启动图像捕获进程(作为 Node 中的子进程)。因此,您可以将其与在子进程中运行的某种无限循环进行比较。到目前为止一切顺利,这对我有用。

但是现在,如果用户再次按下按钮,子进程应该停止,这样图像捕获就不会再继续了。我的问题是,我不知道如何在不完全终止子进程的情况下从主进程中停止子进程(用户应该能够再次启动循环)。

这是主流程的一个片段(“start”是 true 或 false,取决于用户是否按下了开始按钮。每次用户单击“start”按钮都会更改其状态):

var aCapLive = child.fork(__dirname + "/cp_capture_liveimg_async.js");  //Livebild
if (start) aCapLive.send(start);
else aCapLive.send(start);

这是来自 child 的:

var i = 0;
process.on("message", function(message)
{
    while(message)
    {
        console.log("CP aCapLive received message: Iteration " + i++);
    }
    console.log("CP aCapLive is going to exit now...");
    process.exit();
}

显然这对我不起作用,因为我正在循环中运行并且无法检测“消息”中的任何更改。如果有人有一些想法,我将非常感激:)

最佳答案

您的代码永远不会收到多于一条消息,因为它会立即进入无限循环,永远不会将控制权返回给事件循环。 message 变量永远不会神奇地改变,而且,由于 Node 是单线程的,除非第一条消息评估为 false,否则子 Node 不可能退出。

我认为你想要更多类似这样的东西:

var i = 0;
process.on("message", function(message)
{
    if(message)
    {
        console.log("CP aCapLive received message: Iteration " + i++);
    }
    else
    {
        console.log("CP aCapLive is going to exit now...");
        process.exit();
    }
});

如果您想要无限循环,则必须在每次迭代后将控制返回到事件循环。这将为新消息提供进入的机会。示例:

var _message;
var _running = false;
process.on("message", function (message)
{
    _message = message; // update the _message var when a new one comes in
    if (!_running) // this just prevents multiple loops from being started.
    {
        _running = true;
        run();
    }
});

var i = 0;
function run ()
{
    if(_message)
    {
        console.log("CP aCapLive received message: Iteration " + i++);
        setImmediate(run); // <-- this causes an infinite non-blocking loop
    }
    else
    {
        console.log("CP aCapLive is going to exit now...");
        process.exit();
    }
}

如果消息每次都相同,那么您真正想要的是这样的切换系统:

var _message = false;
process.on("message", function ()
{
    _message = !_message; // update the _message var when a new one comes in
    if (_message)
        run();
});

var i = 0;
function run ()
{
    if(_message)
    {
        i++;
        if (i % 10000 === 0)
            console.log("CP aCapLive received message: Iteration " + i);
        setImmediate(run); // <-- this causes an infinite non-blocking loop
    }
    else
    {
        console.log("CP aCapLive is going to exit now...");
        process.exit();
    }
}

这是我用来测试它的父进程代码:

var childProcess = require('child_process');

var proc = childProcess.fork('child.js');
proc.send(true);
setTimeout(function () { proc.send(true); }, 4000); // send second message

输出:

CP aCapLive received message: Iteration 10000
CP aCapLive received message: Iteration 20000
CP aCapLive received message: Iteration 30000

... full output omitted for brevity ...

CP aCapLive received message: Iteration 1660000
CP aCapLive received message: Iteration 1670000
CP aCapLive received message: Iteration 1680000
CP aCapLive is going to exit now...

关于javascript - 仅在 Nodejs 中需要时才从主进程启动和停止子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17619748/

相关文章:

javascript - 如何将值传递到 HTML 模板侧边栏?

javascript - 如何使用 WWW::Mechanize 访问按下拉列表分割的页面

javascript - 获取 API POST 请求响应

c# - 当路径包含 url 片段时,如何在网络浏览器中打开本地 html 文件

c# - 我怎样才能开始一个新的流程并等到它完成?

javascript - 回流时测试 JS 是否启用?

node.js - 如何将 Sequelize 查询的输出传递给另一个将使用 express 返回 JSON 对象的函数?

node.js - 如何向第三方服务器发出 promise 调用?

node.js - 结合使用AWS Lambda和Elastic Search,从搜索客户端获取未定义

c# - Process.Start 找不到可执行文件