Node.js pass handle of response object handle to child process

孤者浪人 提交于 2019-12-01 01:04:34

问题


I have an http server and a forked child process. I want the parent to receive requests and pass to forked process using worker.send. and the worker should be able to process and send the response back to the requester using the same response object.

I tried sending the response object in the second parameter of worker.send, but it gives the error This handle type can't be sent

var child_process = require('child_process');

var worker = child_process.fork(filename);

http.createServer(function (req, res) {
    worker.send({ 'event': 'start' }, res); // send response object
}).listen(4000);

I checked in the child_process.js file and it says if it doesn't belongs to some of the types, it will throw the error.

I want to know if there are any other options by which I can send the response object to the forked child.

EDIT:

Okay, here's what I found, I just changed the following

// Instead of
// worker.send({ 'event': 'start' }, res);
worker.send({ 'event': 'start' }, res.socket);

And the forked process is able to call write on the handler it gets.

Is it correct? Can I use it this way? or will there be any implications under some blah blah conditions?


回答1:


The worker.send only takes requests coming from a net.createServer function. Try better with 'net' package instead of http. This resolved my issue



来源:https://stackoverflow.com/questions/17675101/node-js-pass-handle-of-response-object-handle-to-child-process

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!