Skip to content

Instantly share code, notes, and snippets.

@dansteingart
Created November 1, 2012 01:09

Revisions

  1. dansteingart created this gist Nov 1, 2012.
    35 changes: 35 additions & 0 deletions Spawner.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    //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];


    })