Created
November 1, 2012 01:09
-
-
Save dansteingart/3990987 to your computer and use it in GitHub Desktop.
Keeping Tabs On Multiple Spawned Processes in NodeJS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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