简体   繁体   English

分叉的子进程始终以代码1终止

[英]Forked child process keeps terminated with code 1

I wrapped a module using Electron Packager. 我使用Electron Packager包装了一个模块。 Because it has heavy computation, i put it in a sub process that would be fork ed from renderer.js when user clicks a button on index.html . 由于计算量很大,因此我将其放在一个子进程中,当用户单击index.html上的按钮时,该子进程将从renderer.js fork出来。

Pseudo-code renderer.js from : 来自的伪代码renderer.js

let cp = require('child_process');
let subprocess;
function log(msg) {
    // A function to log messages sent from subprocess
}
document.querySelector('#create').addEventListener('click', ev => {
  subprocess = cp.fork('./subprocess.js');
  log('A subprocess has been created with pid: ' + subprocess.pid + ' with exexPath = ' + process.execPath);
  subprocess.on('exit', (code, signal) => log(`child process terminated: signal = ${signal} ; code = ${code}`));
  subprocess.on('error', log);
  subprocess.on('message', log);
});

The real problem is: this subprocess runs smoothly when i call electron ./ from console in working directory, but the build generated by Electron Packager wouldn't. 真正的问题是:当我从工作目录中的控制台调用electron ./时,此子进程运行平稳,但是由Electron Packager生成的构建却无法运行。

The subprocess does not show up in Task Manager, or rather, it is terminated as soon as it appears. 子进程未显示在任务管理器中,或者,子进程一出现便被终止。 The log says child process terminated: signal = null ; code = 1 日志显示child process terminated: signal = null ; code = 1 child process terminated: signal = null ; code = 1 . child process terminated: signal = null ; code = 1

Although i guarded at the beginning of subprocess.js with this to catch uncaughtException 虽然我在subprocess.js的开头进行了监视以捕获uncaughtException

process.on('uncaughtException', (err) => {
   process.send(`Caught exception: ${err}`);
});

Nothing is recorded in log. 什么都没有记录在日志中。 What should i do to overcome this situation? 我应该怎么做才能克服这种情况?

System specs : 系统规格

  • Window 10 视窗10
  • Node 8.6 节点8.6
  • Electron 1.7.12 电子1.7.12
  • Electron Packager 10.1.2 电子包装机10.1.2

I have experienced this too. 我也经历过 One reason i came up with was because the child process will be the child process of electron itself. 我想到的一个原因是,子过程将是电子本身的子过程。 In my case, it will not recognize the node modules i defined. 就我而言,它将无法识别我定义的节点模块。 I suggest using spawn with the spawn process being node.exe. 我建议在生成过程为node.exe的情况下使用spawn。 But that will not be practical once you build your app. 但这在您构建应用程序后将不切实际。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM