Piping data from child to parent in nodejs

前端 未结 3 1427
你的背包
你的背包 2020-12-31 11:28

I have a nodejs parent process that starts up another nodejs child process. The child process executes some logic and then returns output to the parent. The output is large

3条回答
  •  温柔的废话
    2020-12-31 11:53

    I was running into the same issue and used the {end:false} option to fix the error. Unfortunately the accepted answer works only while handling discrete writes of short amounts of data. In case you have a lot of data (rather than just short messages), you need to handle flow control and using the .write() is not the best. For scenarios like this (large data transfers), its better you use the .pipe() function as originally in your code to handle flow control.

    The error is thrown because the readable stream in your parent process is trying to end and close the writable stream input pipe of your child process. You should use the {end: false} option in the parent process pipe:

    Original Code: require('streamifier').createReadStream('test 2').pipe(child.stdio[3]);

    Suggested Modification: require('streamifier').createReadStream('test 2').pipe(child.stdio[3], {end:false});

    See details here from the NodeJs documentation: https://nodejs.org/dist/latest-v5.x/docs/api/stream.html#stream_readable_pipe_destination_options

    Hope this helps someone else facing this problem.

提交回复
热议问题