Here is my problem. I implemented a small script that does some heavy computing, like the node.js. module So, if I type "node myModule.js", it calculates for a second and then returns a value. Now I want to use this module from my main node.JS program. I could just put all the calculations in the "doSomeCalculation" function and then do:
var myModule = require("./myModule"); myModule.doSomeCalculation();
But it will block, so it would be bad. I would like to use it in a non-blocking way, for example, for example, DB calls. So I tried using child_process.spawn and exec, for example:
var spawn = require("child_process").spawn; var ext = spawn("node ./myModule.js", function(err, stdout, stderr) { }); ext.on("exit", function() { console.log("calculation over!"); });
But of course this will not work. I tried to use an EventEmitter in myModule by emitting "calculateDone" events and trying to add a linked listener to the "ext" variable in the above example. Still not working.
As for the forks, they are not exactly what I am trying to do. Forks will require the input of the code related to the calculation into the main program, forcing, the calculation in the child, while the parent does everything that he does, and then how can I return the result?
So here is my question: can I use a child process to perform a non-blocking calculation when the calculation is placed in a Node file, or is it just not possible? Should I do a heavy calculation in a Python script instead? In both cases, how to pass arguments to a child process - for example, an image?
user1822364 Nov 14 2018-12-11T00: 00Z
source share