John Smith
John Smith

Reputation: 6259

Pass Buffer to ChildProcess Node.js

Here I have on Node.Js where I want to do Image Processing in a Sub Process.

As you will see I take the file image.jpg and want to write it back to hello.jpg in a subprocess:

var node = require('child_process').spawn('node',['-i']);
var fs = require('fs');

node.stdout.on('data',function(data) {
  var fs = require('fs');
    var gm = require('gm').subClass({ imageMagick: true });

    gm(data)
     .resize(500, 500)
     .toBuffer("jpg", function(err, buffer) {
      if (err) {
        console.log(err);
      }else{
        fs.writeFile("hello.jpg", buffer);  
      }
   });
});


var buffer = fs.readFileSync(__dirname + "/image.jpg");
node.stdin.write(buffer);

However when I run this file I get this error:

[Error: Stream yields empty buffer]

For me it seems like the buffer is not passed correctly to the subprocess? What do I wrong? What can I do to run Image Processing in a subtask. For me its important that Its not read from a file in the subprocess. Because I want to read one File again and then send the buffer to several subprocesses that do Image Transformations. Thanks!

Upvotes: 1

Views: 1308

Answers (1)

eush77
eush77

Reputation: 4088

You are not doing any work in a subprocess. It is just node -i and nothing else. All your image processing happens in the main process.

To fix it, you can actually run another Node process and give it some script to execute, say worker.js:

process.stdin.on('data',function(data) {
  var fs = require('fs');
    var gm = require('gm').subClass({ imageMagick: true });

    gm(data)
     .resize(500, 500)
     .toBuffer("jpg", function(err, buffer) {
      if (err) {
    console.log(err);
      }else{
    fs.writeFile("hello.jpg", buffer);
      }
   });
});

Then you would create a subprocess from your main script:

var node = require('child_process').spawn('node', ['worker.js']);
var fs = require('fs');

var buffer = fs.readFileSync(__dirname + "/image.jpg");
node.stdin.end(buffer);

Note that I used node.stdin.end in the last line to terminate the worker.

Take a look at cluster module for the alternative approach.

Upvotes: 3

Related Questions