socket.io(nodejs)发出回调是否会通过在回调完成期间阻止服务器来影响服务器性能? [英] Does socket.io (nodejs) emit callback affects server performance by blocking the server during the callback completion?

查看:108
本文介绍了socket.io(nodejs)发出回调是否会通过在回调完成期间阻止服务器来影响服务器性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Say A向B发送消息并等待回调然后A可能发送新消息而B也向A发送许多消息。

Say A sends a message to B and waits for a callback and then A probably sends new messages and B also sends many messages to A.

我的意思是其他在回调发生之前的这段时间内发生了消息交换。

What I mean other message exchanges happen during this time before callback happens.

它是否会创建竞争条件或阻止其他消息发送,直到第一个回调完成或它是否强制执行回调顺序,以便说出消息1,2的回调,3,4,5总是以与发送消息相同的顺序到达?

Does it create a race condition or block other message sending until the first callback is complete or does it enforce the order of callbacks so that say the callbacks of message 1,2,3,4,5 always arrive in the same order as the message was sent out?

非常感谢协助。

推荐答案

嗯,这个问题涉及很多概念 - 很难完全回答。我会尽量给予部分回应或洞察力。
如果您提供了详细信息,为什么它对您的目的很重要 - 它可以帮助更好地确定答案。

Well, the question involves a number of concepts - so hard to answer fully. I will try to t least give partial response or insight. If you had provided details, why it matters for your purpose - it could help to better target the answer.

nodejs的一个优点是它是一个单线程,事件驱动的非阻塞I / O模型 - 这意味着几乎没有或最小阻止(至少在理论上)。 在此处查看概念模型

One of the advantages of nodejs is that it is a single-threaded, event-driven, non-blocking I/O model - which means there is almost no or minimum blocking (at least theoretically). See conceptual model here

但是,由于传输,一致性等原因,应该发生一些微不足道的阻塞[?]。但这应该不是一个问题,因为这将是非常微不足道的,并且无论使用何种语言都会在所有程序中发生。
其次关于套接字。 socket的概念认为,根据您的目的,它可以是阻塞的或非阻塞的。 阻止和非阻塞套接字
阻止并不一定意味着它很糟糕。

However, some trivial blocking should happen due to transport, consistency etc [?]. But it shouldn't be a problem as this will extremely insignificant and happens in all programs no matter what language it uses. Secondly about sockets. The concept of socket considers thatit can be blocking or non-blocking depending on your purpose. Blocking and Non-blocking sockets Blocking doesnt necessarily mean it is bad.

第三,即使没有阻塞,仍然没有事件并行发生。我的意思是即使A和B非常频繁地向对方发送消息 - 它们之间存在时间差 - 尽管对人类来说是微不足道的。甚至可以在百万分之一秒内表达这种差异。你真的可以在一秒钟内发送超过一百万条消息吗?因此,即使回调有一些影响 - 你应该为了你的程序而忽略它。而且,即使它们同时出现,javascript也可以一次做一件事 - 所以在收到时,你应该一次做一件事。例如,如果要显示或提醒消息,则它们将一次显示一条消息。

Thirdly, even if there is no blocking, still events dont really happen in parallel. I mean even if A and B send messages to each other very frequently - there is a time gap between them - although trivial for humans. That difference can even be expressed even in millionth of second. Can you really send over million messages in a second? So, even if callback has some impact - you should ignore it for the purpose of your program. Also, even if they occur at the same time, javascript can do one thing at a time - so at the end when you receive, you should do them one at a time. For example, if you want to display or alert a message, they will be one at a time.

关于消息的排序,Node.js是单个事件循环。所以,我的理解是它运行一个不停的循环并等待事件,并按事件发生的顺序发出信息。例如了解nodejs事件循环

As to ordering of the messages, Node.js is a single event loop. So, my understanding is it runs a non-stop loop and waits for events, and emits information in the order the events occur. For example Understanding nodejs event loop

 while(new Date().getTime() < now + 1000) { // do nothing }

所以,为了你的目的,我会说除非B在A发送消息和接收它的服务器之间发送消息,你应该先收到回调。简单地按照nodejs服务器接收它的顺序进行排序。希望能帮助到你。

So, for your purpose, I would say unless B sends a message between A sending a message and server receiving it, you should receive a callback before anything else. Simply ordering happens in the order the nodejs server receives it. Hope it helps.

这篇关于socket.io(nodejs)发出回调是否会通过在回调完成期间阻止服务器来影响服务器性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆