【问题标题】:child_process.execFile slow to exitchild_process.execFile 退出缓慢
【发布时间】:2018-09-14 07:48:13
【问题描述】:

我有一个 Node 脚本,它以这种方式调用外部程序 (PluginManager.exe):

const util = require('util');
const execFile = util.promisify(require('child_process').execFile);

const process = execFile('PluginManager.exe', ['/install']);
process
  .then(({stdout, stderr}) => console.log('done', stdout, stderr))
  .catch(e => console.log(e));

PluginManager.exe 需要 8 秒来执行。我的问题是节点脚本在子进程退出后继续运行另外 10 秒。我知道PluginManager.exe 何时完成,因为我可以看到它从 Windows 任务管理器进程列表中消失。

是什么让 Node 进程运行这么长时间?我可以做些什么来确保它在子进程退出后立即退出?

【问题讨论】:

  • 你可以试试execFile('PluginManager.exe', ['/install'], {shell: true})看看有没有帮助?
  • @TarunLalwani 感谢您的建议,但我得到了相同的结果。
  • 您使用的是哪个版本的 Node?​​span>
  • 是否有任何衍生的进程有这个问题?有exe吗?如果我们不能复制,就很难提供解决方案。
  • @KevinPeno,是的,它是exe。有趣的是,并不是所有的exes 都这样做!我会做更多的测试来找到一种模式并确定是哪种种类 exe 导致了这种情况。感谢您的评论。

标签: node.js windows child-process


【解决方案1】:

您是否尝试过将killSignal 选项设置为更具侵略性的设置?

const process = execFile('PluginManager.exe', ['/install'], {killSignal: 'SIGKILL'});

【讨论】:

    【解决方案2】:

    也许它正在等待输入并在 10 秒后超时?

    尝试使用.end() 关闭标准输入,如https://nodejs.org/api/child_process.html#child_process_subprocess_stdin 中所述

    (在这种用法中,您需要execFile 的原始返回值,所以不要承诺,按照https://stackoverflow.com/a/30883005/1105015

    例如

    const util = require('util');
    const execFile = require('child_process').execFile;
    
    const process = execFile(
      'PluginManager.exe', ['/install'], (e, stdout, stderr) => {
        if (e) {
          console.log(e);
        } else {
          console.log('done', stdout, stderr));
        }});
    process.stdin.end();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-17
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2016-08-28
      相关资源
      最近更新 更多