Skip to content Skip to sidebar Skip to footer

Node.js Forked Pipe

I am attempting to fork a node.js module as a child process per the example posted on this stackoverflow question. The fork itself works, but the problem I am running into is that

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 a stdio option. You should probably use silent: true instead; which calls spawn with the stdio option set to "pipe".
  • You try to read from a writable stream (stdin): tChild.stdin.on('data', ... You probably want to use stdout.

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 is false)

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"