gpt4 book ai didi

node.js - 有没有办法保持 Node js 集群 send() 消息的顺序

转载 作者:太空宇宙 更新时间:2023-11-04 01:36:38 24 4
gpt4 key购买 nike

使用下面一些添加的序列检查,我发现消息有时会无序到达,从而破坏代码。我想我必须在接收时对无序消息进行排队,以确保事情按顺序处理。这就是 NodeJS 的本质吗?

// In the master process:
msg.sequence = next_sequence[i]++;
worker[i].send(msg)

// In worker(s):
process.on("message",handler);
....
var last_sequence = 0;
function handler(msg){
if ( last_sequence + 1 != msg.sequence ) console.log(...);
last_sequence = msg.sequence;

最佳答案

在接收时使用 send(JSON.stringify(msg)) 和 JSON.parse 后,行为看起来更具确定性,并且消息序列号是有序的。

看来send()并没有立即复制数据,并且在调用send()后不久仍然可以更改数据。

谁能证实这一点?

关于node.js - 有没有办法保持 Node js 集群 send() 消息的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54375528/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com