exec async now returns child proc obj

This commit is contained in:
Artur Adib 2012-05-24 22:10:21 -04:00
Родитель 023bffce6f
Коммит 38a0bde0a9
3 изменённых файлов: 19 добавлений и 7 удалений

Просмотреть файл

@ -322,12 +322,17 @@ Examples:
```javascript ```javascript
var version = exec('node --version', {silent:true}).output; var version = exec('node --version', {silent:true}).output;
var child = exec('some_long_running_process', {async:true});
child.stdout.on('data', function(data) {
/* ... do something with data ... */
});
``` ```
Executes the given `command` _synchronously_, unless otherwise specified. Executes the given `command` _synchronously_, unless otherwise specified.
When in synchronous mode returns the object `{ code:..., output:... }`, containing the program's When in synchronous mode returns the object `{ code:..., output:... }`, containing the program's
`output` (stdout + stderr) and its exit `code`. Otherwise the `callback` gets the `output` (stdout + stderr) and its exit `code`. Otherwise returns the child process object, and
arguments `(code, output)`. the `callback` gets the arguments `(code, output)`.
**Note:** For long-lived processes, it's best to run `exec()` asynchronously as **Note:** For long-lived processes, it's best to run `exec()` asynchronously as
the current synchronous implementation uses a lot of CPU. This should be getting the current synchronous implementation uses a lot of CPU. This should be getting
@ -372,4 +377,3 @@ Returns true if all the given paths exist.
_This function is being deprecated. Use `silent(false) instead.`_ _This function is being deprecated. Use `silent(false) instead.`_
Enables all output (default) Enables all output (default)

Просмотреть файл

@ -812,12 +812,17 @@ exports.env = process.env;
//@ //@
//@ ```javascript //@ ```javascript
//@ var version = exec('node --version', {silent:true}).output; //@ var version = exec('node --version', {silent:true}).output;
//@
//@ var child = exec('some_long_running_process', {async:true});
//@ child.stdout.on('data', function(data) {
//@ /* ... do something with data ... */
//@ });
//@ ``` //@ ```
//@ //@
//@ Executes the given `command` _synchronously_, unless otherwise specified. //@ Executes the given `command` _synchronously_, unless otherwise specified.
//@ When in synchronous mode returns the object `{ code:..., output:... }`, containing the program's //@ When in synchronous mode returns the object `{ code:..., output:... }`, containing the program's
//@ `output` (stdout + stderr) and its exit `code`. Otherwise the `callback` gets the //@ `output` (stdout + stderr) and its exit `code`. Otherwise returns the child process object, and
//@ arguments `(code, output)`. //@ the `callback` gets the arguments `(code, output)`.
//@ //@
//@ **Note:** For long-lived processes, it's best to run `exec()` asynchronously as //@ **Note:** For long-lived processes, it's best to run `exec()` asynchronously as
//@ the current synchronous implementation uses a lot of CPU. This should be getting //@ the current synchronous implementation uses a lot of CPU. This should be getting
@ -837,7 +842,7 @@ function _exec(command, options, callback) {
}, options); }, options);
if (options.async) if (options.async)
execAsync(command, options, callback); return execAsync(command, options, callback);
else else
return execSync(command, options); return execSync(command, options);
}; };
@ -1279,6 +1284,8 @@ function execAsync(cmd, opts, callback) {
if (!options.silent) if (!options.silent)
process.stdout.write(data); process.stdout.write(data);
}); });
return c;
} }
// Hack to run child_process.exec() synchronously (sync avoids callback hell) // Hack to run child_process.exec() synchronously (sync avoids callback hell)

Просмотреть файл

@ -66,8 +66,9 @@ shell.cd('../..');
// //
// no callback (no need for asyncFlags) // no callback (no need for asyncFlags)
shell.exec('node -e \"console.log(1234)\"', {async:true}); var c = shell.exec('node -e \"console.log(1234)\"', {async:true});
assert.equal(shell.error(), null); assert.equal(shell.error(), null);
assert.ok('stdout' in c, 'async exec returns child process object');
var asyncFlags = []; var asyncFlags = [];