Friday, 11 March 2022

Execution of arbitrary JavaScript code (not command) in NodeJS child process

What is the easiest way to run some code (not command!) in a different process and communicate its result with the main process?

So I have a quite intensive task that needs to be split up into different processes. What's the easiest way to do something like this?

// in main process
var otherProcess = createAnotherProcess(function() {
    console.log("this code is ran in another process");
    return "some data";
});
otherProcess.on("done", function(data) {
    console.log(data); // will output "some data"
});

Having a single source code file that is able to run code in multiple processes would be amazing! Is this even possible? I've tried reading a bit about "child_processes" in node but find it a little too convoluted.

Any help?



from Execution of arbitrary JavaScript code (not command) in NodeJS child process

No comments:

Post a Comment