Skip to content

Instantly share code, notes, and snippets.

@dansteingart
Created November 1, 2012 01:09
Show Gist options
  • Save dansteingart/3990987 to your computer and use it in GitHub Desktop.
Save dansteingart/3990987 to your computer and use it in GitHub Desktop.
Keeping Tabs On Multiple Spawned Processes in NodeJS
//First make placeholders for results as globals
results = {}
processes = {}
//Now Spawn Processes within code you need, where index is an identifier
spawn_list[index] = spawn(/*file here*/)
processes[index] = spawn_list[index].pid
results[processes[index]] = {}
results[processes[index]]['index'] = index //funny looking but important
results[processes[index]]['stdout'] = ""
results[processes[index]]['stderr'] = ""
spawn_list[index].stdout.on('data',function(data){results[processes[index]]['stdout'] += data})
spawn_list[index].stderr.on('data',function(data){results[processes[index]]['stderr'] += data})
spawn_list[page_name].on('exit',function(code)
{
this_pid = this.pid
console.log(this_pid)
console.log(code)
stdout = results[this_pid]['stdout']
stderr = results[this_pid]['stderr']
this_index = results[this_pid]['index']
end_time = new Date().getTime()
big_out = {'out':stdout,'outerr':stderr}
//do something with bigoutput
//......
//Clean up, clean up, everybody clean up
delete processes[this_index];
delete spawn_list[this_index];
delete results[this_index];
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment