socket.io(nodejs)emit回调是否影响服务器性能?喜欢它在回调完成期间阻止服务器吗? [英] Does socket.io (nodejs) emit callback affects server perormance? Like does it block the server during the callback completion?

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

问题描述

Say A向B发送消息,并等待回叫。然后A可能发送新消息,B也发送许多消息到A.我的意思是其他消息交换发生在回调发生之前的这段时间。它是否创建一个竞争条件或阻止其他消息发送,直到第一个回调完成?或者它是否强制回调的顺序,以便说消息1,2,3,4,5的回调总是以与发送消息相同的顺序到达?非常感谢。

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. 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? Assistance would be much appreciated.

推荐答案

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

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

然而,由于传输,一致性等,一些小的阻塞应该发生[?]。但它不应该是一个问题,因为这将是非常不重要的,发生在所有的程序,无论使用什么语言。
其次关于套接字。套接字的概念认为根据您的目的可以是阻塞或非阻塞。 阻止和非阻止套接字
阻止不一定意味着它是坏的。

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在发送消息和服务器接收之间发送消息,你应该在任何事情之前收到一个回调。简单的排序发生在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)emit回调是否影响服务器性能?喜欢它在回调完成期间阻止服务器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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