Anonymous
Anonymous

Reputation: 557

Working with child processes

So, I want node to open several hundred of the same process and be able to receive the response from each process and do something with it. I've not used node before...

I've tried this:

var Spawner = require('child_process');

for (x=0; x<100; ++x)
{
    MyApplication = Spawner.spawn('HandShake.exe'); 
}

MyApplication.stdout.on('data', function(data) {
    console.log('stdout: ' + data);
});

However, I only get 1 response to stdout as I belive the var is being overwritten each time.

If possible, how do I go about getting a response from each child process I spawn?

Thanks.

Upvotes: 0

Views: 88

Answers (1)

cr0
cr0

Reputation: 617

Yes you are correct. Your variable is overwritten in each run. As a result you're only listening on the last instance. Add the listener in the loop.

var Spawner = require('child_process');

for (x=0; x<100; ++x)
{
    MyApplication = Spawner.spawn('HandShake.exe'); 
    MyApplication.stdout.on('data', function(data) {
        console.log('stdout' + data);
    });
}

Upvotes: 3

Related Questions