【问题标题】:How to kill a NodeJS child exec process?如何杀死 NodeJS 子执行进程?
【发布时间】:2022-01-21 15:36:18
【问题描述】:

我正在使用 child_processexec 函数从 NodeJS 执行 Python 程序,我希望在单击按钮。 我正在使用 Windows11。

这是我的代码:

var process__ = undefined; // this is a global variable

    function executePython(command_line_arguement){
      const exec = require('child_process').exec;
      process__=exec(`python program.py "${command_line_arguement}"`, { encoding: 'utf-8',detached : true }, (error, stdout, stderr) => {
        if (error) {
          console.error(`exec error: ${error}, ${stderr}`);
        }else{
        console.log(stdout);
        }
      });

    function onButtonClick(){
        process__.kill('SIGINT');
    }

但是,这不会似乎停止杀死被触发的python进程。

任何关于如何进行的指导都将是可观的。

提前致谢!

【问题讨论】:

    标签: python node.js exec child-process kill-process


    【解决方案1】:

    我相信 Windows 需要强制终止来终止进程,例如

    exec('taskkill /F /T /PID ' + process__.pid);
    
    // F - force terminate
    // T - kill all sub-processes
    // PID - Process Id that you're targetting
    
    

    有关 taskkill 和标志,请参阅 documentation

    【讨论】:

      猜你喜欢
      • 2014-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2019-07-13
      • 2015-10-17
      相关资源
      最近更新 更多