Skip to content

Instantly share code, notes, and snippets.

@leeola
Created December 10, 2011 18:43
Show Gist options
  • Save leeola/1455914 to your computer and use it in GitHub Desktop.
Save leeola/1455914 to your computer and use it in GitHub Desktop.
var http = require('http');
var exec = require("child_process").exec;
http.createServer(function (request, response) {
console.log('Response being generated..');
response.writeHead(200, {'Content-Type': 'text/plain'});
call_execs(response);
}).listen(8000);
function call_execs(response) {
// Two var's to store output in.
var output_a;
var output_b;
console.log('Calling blocking code..');
// Call our blocking code
exec("ls /home",
{ timeout: 10000, maxBuffer: 20000*1024 },
function (error, stdout, stderr) {
output_a = stdout;
exec_complete();
});
exec("find /home",
{ timeout: 10000, maxBuffer: 20000*1024 },
function (error, stdout, stderr) {
output_b = stdout;
exec_complete();
});
function exec_complete() {
console.log('Exec complete!');
// Check to see if both execs were written to.
if (output_a !== undefined && output_b !== undefined) {
console.log('Both execs complete!');
response.end('Output A: '+ output_a +'\n\n\nOutput B: '+ output_b);
}
}
}
@chrisdickinson
Copy link

just a sketch of how i'd approach the problem:

function call_execs(response) {
    // Two var's to store output in.
    var output = {}
      , check_done = []
      , store = function(into) {
        check_done.push(into)
        return function(error, stdout, stderr) {
          // remove the check
          check_done.splice(check_done.indexOf(into), 1)
          output[into] = stdout
          if(check_done.length === 0) {
            exec_complete()
          }
        }
      }
      , config = {timeout:10000, maxBuffer:20000*1024}

    console.log('Calling blocking code..');
    // Call our blocking code
    exec("ls /home", config, store('a'))
    exec("find /home", config, store('b'))

    function exec_complete() {
      console.log('All execs complete!');
      response.end('Output A: '+ output.a +'\n\n\nOutput B: '+ output.b);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment