简体   繁体   中英

Forked child process keeps terminated with code 1

I wrapped a module using 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 .

Pseudo-code renderer.js from :

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.

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 .

Although i guarded at the beginning of subprocess.js with this to catch 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
  • Node 8.6
  • Electron 1.7.12
  • Electron Packager 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. But that will not be practical once you build your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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