Node.js将响应对象句柄的句柄传递给子进程 [英] Node.js pass handle of response object handle to child process
问题描述
我有一个 http
服务器和一个分叉的子进程。我希望父级接收请求并使用 worker.send
传递给分叉进程。并且worker应该能够使用相同的响应对象处理并将响应发送回请求者。
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.
我尝试在<$的第二个参数中发送响应对象c $ c> worker.send ,但它给出错误此句柄类型无法发送
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);
我检查了child_process.js文件,并说它是否属于某些类型,它会抛出错误。
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.
编辑:
好的,这是我发现的,我只是更改了以下内容
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?
推荐答案
worker.send仅接收来自net.createServer的请求功能。使用'net'包而不是http更好地尝试。这解决了我的问题
The worker.send only takes requests coming from a net.createServer function. Try better with 'net' package instead of http. This resolved my issue
这篇关于Node.js将响应对象句柄的句柄传递给子进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!