An object representing a child process.

The ChildProcess class is not intended to be used directly. Use the spawn() or fork() module methods to create a ChildProcess instance.

Variables

read onlyconnected:Bool

Set to false after disconnect' is called If connected` is false, it is no longer possible to send messages.

read onlypid:Int

The PID of the child process.

read onlystderr:IReadable

A Readable Stream that represents the child process's stderr.

If the child stdio streams are shared with the parent, then this will not be set.

read onlystdin:IWritable

A Writable Stream that represents the child process's stdin. Closing this stream via end often causes the child process to terminate.

If the child stdio streams are shared with the parent, then this will not be set.

read onlystdio:Array<IStream>

The parent end of the stdio pipes.

read onlystdout:IReadable

A Readable Stream that represents the child process's stdout.

If the child stdio streams are shared with the parent, then this will not be set.

Methods

disconnect():Void

Close the IPC channel between parent and child, allowing the child to exit gracefully once there are no other connections keeping it alive.

After calling this method the connected flag will be set to false in both the parent and child, and it is no longer possible to send messages.

The 'disconnect' event will be emitted when there are no messages in the process of being received, most likely immediately.

Note that you can also call process.disconnect in the child process.

kill(?signal:String):Void

Send a signal to the child process.

If no argument is given, the process will be sent 'SIGTERM'. See signal(7) for a list of available signals.

May emit an 'error' event when the signal cannot be delivered.

Sending a signal to a child process that has already exited is not an error but may have unforeseen consequences: if the PID (the process ID) has been reassigned to another process, the signal will be delivered to that process instead. What happens next is anyone's guess.

Note that while the function is called kill, the signal delivered to the child process may not actually kill it. kill really just sends a signal to a process. See kill(2)

send(message:Dynamic, ?callback:Error ‑> Void):Bool

send(message:Dynamic, sendHandle:Dynamic, options:ChildProcessSendOptions, ?callback:Error ‑> Void):Bool

send(message:Dynamic, sendHandle:Dynamic, ?callback:Error ‑> Void):Bool

When using fork you can write to the child using send and messages are received by a 'message' event on the child.

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.

Please note that the send method on both the parent and child are synchronous - sending large chunks of data is not advised (pipes can be used instead, see spawn).

There is a special case when sending a {cmd: 'NODE_foo'} message. All messages containing a NODE_ prefix in its cmd property will not be emitted in the 'message' event, since they are internal messages used by node core. Messages containing the prefix are emitted in the 'internalMessage' event, you should by all means avoid using this feature, it is subject to change without notice.

The sendHandle option is for sending a TCP server or socket object to another process. The child will receive the object as its second argument to the message event.

The callback option is a function that is invoked after the message is sent but before the target may have received it. It is called with a single argument: null on success, or an Error object on failure.

Emits an 'error' event if the message cannot be sent, for example because the child process has already exited.

Returns true under normal circumstances or false when the backlog of unsent messages exceeds a threshold that makes it unwise to send more. Use the callback mechanism to implement flow control.

unref():Void

By default, the parent will wait for the detached child to exit. To prevent the parent from waiting for a given child, use the unref method, and the parent's event loop will not include the child in its reference count.

Inherited Variables

Inherited Methods

Defined by EventEmitter

addListener<T>(eventName:Event<T>, listener:T):TSelf

emit<T>(eventName:Event<T>, args:Rest<Dynamic>):Bool

Synchronously calls each of the listeners registered for the event named eventName, in the order they were registered, passing the supplied arguments to each.

See also:

eventNames():Array<EitherType<String, Symbol>>

Returns an array listing the events for which the emitter has registered listeners. The values in the array will be strings or Symbols.

See also:

getMaxListeners():Int

Returns the current max listener value for the EventEmitter which is either set by emitter.setMaxListeners(n) or defaults to EventEmitter.defaultMaxListeners.

See also:

listenerCount<T>(eventName:Event<T>):Int

Returns the number of listeners listening to the event named eventName.

See also:

listeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName.

See also:

off<T>(eventName:Event<T>, listener:T):TSelf

on<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the end of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

once<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName. The next time eventName is triggered, this listener is removed and then invoked.

See also:

prependListener<T>(eventName:Event<T>, listener:T):TSelf

Adds the listener function to the beginning of the listeners array for the event named eventName. No checks are made to see if the listener has already been added. Multiple calls passing the same combination of eventName and listener will result in the listener being added, and called, multiple times.

See also:

prependOnceListener<T>(eventName:Event<T>, listener:T):TSelf

Adds a one-time listener function for the event named eventName to the beginning of the listeners array. The next time eventName is triggered, this listener is removed, and then invoked.

See also:

rawListeners<T>(eventName:Event<T>):Array<T>

Returns a copy of the array of listeners for the event named eventName, including any wrappers (such as those created by .once()).

See also:

removeAllListeners<T>(?eventName:Event<T>):TSelf

Removes all listeners, or those of the specified eventName.

See also:

removeListener<T>(eventName:Event<T>, listener:T):TSelf

Removes the specified listener from the listener array for the event named eventName.

See also:

setMaxListeners(n:Int):Void

By default EventEmitters will print a warning if more than 10 listeners are added for a particular event. This is a useful default that helps finding memory leaks. Obviously, not all events should be limited to just 10 listeners. The emitter.setMaxListeners() method allows the limit to be modified for this specific EventEmitter instance. The value can be set to Infinity (or 0) to indicate an unlimited number of listeners.

See also: