将Buffer传递给ChildProcess Node.js

时间:2022-08-03 21:58:24

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

这里我在Node.Js上我想要在Sub Process中进行图像处理。

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

正如您将看到的,我将文件image.jpg想要在子进程中将其写回hello.jpg:

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!

对我来说,似乎缓冲区没有正确传递给子进程?我错了什么?如何在子任务中运行图像处理。对我来说重要的是它不能从子进程中的文件中读取。因为我想再次读取一个文件,然后将缓冲区发送到几个进行图像转换的子进程。谢谢!

1 个解决方案

#1


3  

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.

你没有在子进程中做任何工作。它只是节点-i而没有别的。所有图像处理都在主过程中进行。

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

要修复它,你实际上可以运行另一个Node进程并给它一些脚本来执行,比如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.

请注意,我在最后一行使用了node.stdin.end来终止worker。

Take a look at cluster module for the alternative approach.

看看集群模块的替代方法。

#1


3  

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.

你没有在子进程中做任何工作。它只是节点-i而没有别的。所有图像处理都在主过程中进行。

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

要修复它,你实际上可以运行另一个Node进程并给它一些脚本来执行,比如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.

请注意,我在最后一行使用了node.stdin.end来终止worker。

Take a look at cluster module for the alternative approach.

看看集群模块的替代方法。