javascript - Pass Buffer to ChildProcess Node.js -


here have on node.js want image processing in sub process.

as see take file image.jpg , want write hello.jpg in 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 run file error:

[error: stream yields empty buffer]

for me seems buffer not passed correctly subprocess? wrong? what can run image processing in subtask. me important not read file in subprocess. because want read 1 file again , send buffer several subprocesses image transformations. thanks!

you not doing work in subprocess. node -i , nothing else. image processing happens in main process.

to fix it, can run node process , give script execute, 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 create subprocess 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 used node.stdin.end in last line terminate worker.

take @ cluster module alternative approach.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -