【问题标题】:Node Child process is running continuously without closing after getting the data节点子进程获取数据后持续运行不关闭
【发布时间】:2019-05-14 09:06:38
【问题描述】:

我正在处理一个需要运行后台作业的环回项目。生成节点子进程。由于我是初学者,因此文档有点混乱。 https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

父.js

const path = require("path");

const spawn = require('child_process').spawn;

let child = spawn('node', [__dirname + '../../worker/worker.js']);

child.stdout.on('data', function (data) {
      console.log('data available ' + data);
});

child.stderr.on('data', function (data) {
     console.log('There was an error: ' + data);
});

worker.js

function jobListener() {


    let query = { jobname: "countProvider"}
    let result = Job.findOne({ where: query }, function(err, instance) {
        if(err) {
            console.log('Error', err);
        }
      // console.log(instance);
    });

    while (!result) { 
        console.log(result);
       // await sleep(5000);
        console.log('checking for job in mongodb with a delay of 5 seconds');
    }
    if(result) {
        fetchProcedureRequest();
    }
}

jobListener();

function fetchProcedureRequest() {
    console.log('Fetching pREquest');
}

我想要实现的是我在子进程中获取数据。但是,当父级中的数据可用时,我如何关闭/退出该进程。有人请帮忙,任何建议都将不胜感激。

【问题讨论】:

    标签: javascript node.js child-process dom-events spawn


    【解决方案1】:

    也许你可以使用这个,在这里找到How to kill childprocess in nodejs?

    var proc = require('child_process').spawn('mongod'); proc.kill('SIGINT');

    【讨论】:

    • 我可以在哪里使用它?在这段代码里面? child.stdout.on('data', function (data) { console.log('data available' + data); });
    • 是的,你可以尝试 child.kill('SIGINT') inside child.stdout.on('data' ...
    猜你喜欢
    • 2019-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-09
    • 2012-09-13
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多