0
\$\begingroup\$

So basically my question is what would be a better way to achieve my goal? The code below works fine but looks like it is extremely inefficient and could be achieved in a better way. The code essentially spawns several child processes with a dynamic name and because the eval() function is in the global scope, the child processes are also available in global scope. I need to have the processes available in a global scope since I want to be able to kill each process individually through use of commands. I hope I have explained everything as clear as needed. If there are any questions, feel free to ask. Thanks.

processName = "child";
for (let a = 0; a < deviceAmount; a++) {
  eval(
    `
          var ${processName + a} = child_process.spawn("node", ["slave.js", ${a + 1}]);
      
          ${processName + a}.stdout.setEncoding("utf8");
          ${processName + a}.stdout.on("data", function (data) {
            process.stdout.write(data);
          });
          
          ${processName + a}.stderr.setEncoding("utf8");
          ${processName + a}.stderr.on("data", function (data) {
            process.stderr.write("ERROR: " + data);
          });
          
          ${processName + a}.on("close", function (code) {
            process.stdout.write("Exited with code: " + code);
          });
          `
  );
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

Unless there's some reason the child processes need to have their own names in the global scope, the easier and safer solution would seem to be having a global array which the child processes are kept in. Something kind of like this:

// At the top level
var children = [];

// Possibly elsewhere
for (let a = 0; a < deviceAmount; a++) {
    let child = child_process.spawn("node", ["slave.js", a + 1]);

    child.stdout.setEncoding("utf8");
    child.stdout.on("data", function (data) {
        process.stdout.write(data);
    });
          
    child.stderr.setEncoding("utf8");
    child.stderr.on("data", function (data) {
        process.stderr.write("ERROR: " + data);
    });
          
    child.on("close", function (code) {
        process.stdout.write("Exited with code: " + code);
    });

    children.push(child);
}
\$\endgroup\$
3
  • \$\begingroup\$ That sounds like it could work, but how would I be able to kill a specific individual process if they are all the same name? I'd need to know it's exact position in the array right? \$\endgroup\$
    – WoJo
    May 21, 2022 at 17:26
  • \$\begingroup\$ You'd use children[0] rather than child0. See also "Variable" variables in JavaScript. \$\endgroup\$
    – ggorlen
    May 21, 2022 at 18:56
  • \$\begingroup\$ Thanks. I ended up building a class (in order to be able to add other properties) around the child object and then pushing the class in an array. \$\endgroup\$
    – WoJo
    May 22, 2022 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.