Add silent option to child_process.fork

Fixes #2354.
This commit is contained in:
Andreas Madsen 2011-12-17 11:52:40 +01:00 committed by Ryan Dahl
parent 10d92b3dcd
commit b084322f52
3 changed files with 82 additions and 4 deletions

View File

@ -229,8 +229,9 @@ And then the child script, `'sub.js'` might look like this:
In the child the `process` object will have a `send()` method, and `process`
will emit objects each time it receives a message on its channel.
By default the spawned Node process will have the stdin, stdout, stderr
associated with the parent's.
By default the spawned Node process will have the stdout, stderr associated
with the parent's. To change this behavior set the `silent` property in the
`options` object to `true`.
These child Nodes are still whole new instances of V8. Assume at least 30ms
startup and 10mb memory for each new Node. That is, you cannot create many

View File

@ -158,8 +158,8 @@ exports.fork = function(modulePath, args, options) {
}
// Leave stdin open for the IPC channel. stdout and stderr should be the
// same as the parent's.
options.customFds = [-1, 1, 2];
// same as the parent's if silent isn't set.
options.customFds = (options.silent ? [-1, -1, -1] : [-1, 1, 2]);
// Just need to set this - child process won't actually use the fd.
// For backwards compat - this can be changed to 'NODE_CHANNEL' before v0.6.

View File

@ -0,0 +1,77 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
var common = require('../common');
var assert = require('assert');
var fork = require('child_process').fork;
var isChild = process.argv[2] === 'child';
if (isChild) {
console.log('LOG: a stdout message');
console.error('LOG: a stderr message');
process.send('message from child');
process.once('message', function () {
process.send('got message from master');
});
} else {
var checks = {
stdoutNotPiped: false,
stderrNotPiped: false,
childSending: false,
childReciveing: false
};
var child = fork(process.argv[1], ['child'], { silent: true });
child.on('message', function (message) {
if (checks.childSending === false) {
checks.childSending = (message === 'message from child');
}
if (checks.childReciveing === false) {
checks.childReciveing = (message === 'got message from master');
}
if (checks.childReciveing === true) {
child.kill();
}
});
checks.stdoutNotPiped = (child.stdout && child.stdout.readable === true);
checks.stderrNotPiped = (child.stderr && child.stderr.readable === true);
child.send('message to child');
//Check all values
process.once('exit', function() {
//Check message system
assert.ok(checks.childSending, 'The child was able to send a message');
assert.ok(checks.childReciveing, 'The child was able to receive a message');
//Check std(out|err) pipes
assert.ok(checks.stdoutNotPiped, 'The stdout socket was piped to parent');
assert.ok(checks.stdoutNotPiped, 'The stderr socket was piped to parent');
});
}