Node.js Forked Pipe
Solution 1:
fork
is asynchronous, but its return value is not populated asynchronously. It returns an instance of ChildProcess, that inherits from EventEmitter. An EventEmitter is commonly used for asynchronous tasks, but you receive events when something happens instead of using a callback. Like it says in the docs:
These methods follow the common async programming patterns (accepting a callback or returning an EventEmitter).
In the first example:
fork
does not have astdio
option. You should probably usesilent: true
instead; which callsspawn
with thestdio
option set to "pipe".- You try to read from a writable stream (stdin):
tChild.stdin.on('data', ...
You probably want to usestdout
.
It appears that stdin
and stdout
can be null
depending on if you use silent: true
or not. See options.silent
in the docs for fork
:
Boolean
If true, stdin, stdout, and stderr of the child will be piped to the parent, otherwise they will be inherited from the parent, see the "pipe" and "inherit" options for spawn()'s stdio for more details (default isfalse
)
So the data is just going to your main script's stdout
. You can fix this with (note that your stdio
option wasn't doing anything):
tChild = fork( aPath, [], {silent: true});
As I said earlier, you'll need to listen to the data
event on stdout
.
Post a Comment for "Node.js Forked Pipe"