Node.js nodejs中的子进程fork多次调用
我有点搞不懂为什么连接会多次触发。让我们先从我的代码开始 main.jsNode.js nodejs中的子进程fork多次调用,node.js,express,child-process,Node.js,Express,Child Process,我有点搞不懂为什么连接会多次触发。让我们先从我的代码开始 main.js let Tokens = {}; app.getOauthToken = async function (host, port, oauthcall = false) { if (host !== undefined && port !== undefined) { const key = host + ":" + port; if (Tokens[key] !=
let Tokens = {};
app.getOauthToken = async function (host, port, oauthcall = false) {
if (host !== undefined && port !== undefined) {
const key = host + ":" + port;
if (Tokens[key] !== undefined) {
if(oauthcall){
console.log("already auth token present (mainjs)")
await Tokens[key].getAccessToken(false);
child.send( Tokens[key] )
}
return Tokens[key];
} else {
Tokens[key] = new Token(host, port);
if (oauthcall) {
await Tokens[key].getAccessToken(false);
console.log( " auth token in mainjs ------->", Tokens[key].token)
child.send( Tokens[key] )
}
return Tokens[key];
}
}
}
child.on('message', (m) => {
console.log(" mess from child ")
if (m && m.auth) {
app.getOauthToken(m.host, m.port, m.oauthcall);
}
});
在child.js中
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true });
process.on('message', (m) => {
console.log("msg from parent ------------>", m )
});
第一次尝试输出(main.js的新启动)
第二次尝试输出(在process.on“message”上的child.js 2中获得消息)
第三次尝试输出(在process.on“message”上的child.js中3次获得消息)
有谁能告诉我这是预期的行为,还是我做错了什么,或者是nodejs中的bug
注意:-我没有使用任何“关闭”方法。这是因为没有使用“关闭”而导致的问题吗?您没有显示该方法,但每次发送消息时都会加载该方法:
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true });
process.on('message', (m) => {
console.log("msg from parent ------------>", m )
});
但是,您触发了进程。send
还会在事件侦听器上添加另一个进程
如果需要将进程.on
置于与进程.send
不同的作用域中,则进程.on
只调用一次,并且进程.send
可以在不执行进程.on
的情况下调用
这清楚吗
例如:
function startTestLoop() {
// Set up listener - this must only ever be executed _once_
// `process.on()` adds _another_ listener callback every time it is run.
process.on('message', (m) => {
// This code is the callback. It runs every time the event is fired.
console.log("msg from parent ------------>", m )
});
// Send message every 10 seconds - do this till the cows come home
const handle = setInterval(() =>
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true }),
10 * 1000
)
}
我没有使用任何循环,你的答案不是很清楚。你的意思是我应该每次重置连接吗?在setinterval of time之后调用Child.js。我会在几个小时后检查,让你知道它不工作,它给出的输出与我的示例代码相同,例如:-第一次尝试时处理消息事件触发单次。第二次尝试处理消息事件触发2次,依此类推。。。。。
mess from child
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
already auth token present (mainjs)
msg from parent ------------> { "token" :dsadaq21}
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true });
process.on('message', (m) => {
console.log("msg from parent ------------>", m )
});
function startTestLoop() {
// Set up listener - this must only ever be executed _once_
// `process.on()` adds _another_ listener callback every time it is run.
process.on('message', (m) => {
// This code is the callback. It runs every time the event is fired.
console.log("msg from parent ------------>", m )
});
// Send message every 10 seconds - do this till the cows come home
const handle = setInterval(() =>
process.send({ "auth": true, "host": url.host, "port": url.port, "oauthcall": true }),
10 * 1000
)
}