There are some scenarios, where we would like to have multiple system processes spawned as child processes from nodeJs. For Instance,  if we wanted to do a batch download of resources from some other server etc.

The gist link contains one such example. The simplest way to identify and get result back from each individual spawned processes is to create the task itself as separate class / function enclosure. So we call all these tasks in their context and perhaps add in a callback with specific identifier associated with each spawned task.


const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();


const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();


const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();


const exec = require('child_process').exec;
StartMultipleTasks = () => {
new StartSingleTask(0).download();
new StartSingleErrorTask(1).download();
new StartSingleTask(2).download();
new StartSingleTask(3).download();
new StartSingleTask(4).download();
new StartSingleErrorTask(5).download();
new StartSingleTask(6).download();
new StartSingleErrorTask(7).download();
}
class StartSingleTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
class StartSingleErrorTask {
constructor(index) {
this.index = index;
}
download() {
exec('wget "http://www.w3schools.com/css/trolltunaga.jpg" -O output1.jpg', function (error, stdout, stderr) {
if (error) {
console.log(this.index + " Failed");
return;
}
else
console.log(this.index + " Succeeded.");
}.bind(this));
}
}
StartMultipleTasks();

By Vivek

Leave a Reply