python - 从 Twisted 服务器接收消息时出现延迟

标签 python twisted lag

我有一个非常简单的客户端和服务器(都是使用 Twisted 用 Python 编写的)。服务器循环并每隔 X 毫秒向客户端发送一条消息。服务器在我的 Raspberry Pi 上运行,我的客户端在我的笔记本电脑上运行,两者都连接到我的家庭网络。

服务器:

from twisted.internet import reactor, protocol
from twisted.protocols.basic import NetstringReceiver
from twisted.internet.task import LoopingCall
from datetime import datetime

class MyProtocol(NetstringReceiver):

    def connectionMade(self):
        self.factory.myOnlyProtocol = self

    def connectionLost(self, reason):
        self.factory.myOnlyProtocol = None

class MyFactory(protocol.ServerFactory):

    protocol = MyProtocol

    def __init__(self):
        self.myOnlyProtocol = None
        self.timeStart = datetime.now()
        self.loop = LoopingCall(self.sendSomethingToClient)
        self.loop.start(0.1)
        print 'Server running'

    def sendSomethingToClient(self):
        if self.myOnlyProtocol is not None:
            millis = (datetime.now() - self.timeStart).microseconds / 1000
            print 'Since message sent: {}ms'.format(millis)
            self.timeStart = datetime.now()
            self.myOnlyProtocol.sendString('something')

reactor.listenTCP(1079, MyFactory())
reactor.run()

客户:

from twisted.internet import reactor, protocol
from twisted.protocols.basic import NetstringReceiver
from datetime import datetime

class MyProtocol(NetstringReceiver):

    def __init__(self):
        self.timeStart = datetime.now()

    def connectionMade(self):
        print 'Connected to server'

    def stringReceived(self, data):
        millis = (datetime.now() - self.timeStart).microseconds / 1000
        print 'Since last message: {}ms'.format(millis)
        self.timeStart = datetime.now()


class MyFactory(protocol.ClientFactory):

    protocol = MyProtocol


reactor.connectTCP('192.168.0.7', 1079, MyFactory())
reactor.run()

这工作得很好,直到我开始每 100 毫秒左右发送一次消息,此时客户端开始更加零星地接收消息。这是我运行脚本时看到的内容:

服务器:

Since message sent: 99ms
Since message sent: 99ms
Since message sent: 99ms
Since message sent: 99ms
Since message sent: 99ms
Since message sent: 99ms

客户端(每 1 毫秒发送一条消息):

Since last message: 181ms
Since last message: 0ms
Since last message: 147ms
Since last message: 0ms
Since last message: 188ms
Since last message: 1ms

如果我尝试更快地发送消息,只会加剧问题:

客户端(每 0.5 毫秒发送一条消息):

Since last message: 157ms
Since last message: 0ms
Since last message: 0ms
Since last message: 1ms
Since last message: 154ms
Since last message: 0ms
Since last message: 0ms
Since last message: 0ms 

我不确定 Twisted 发送消息是否有些奇怪,或者它是否是我的家庭网络,或者如何检查它是哪个网络。有什么想法吗?

最佳答案

当您关心 TCP 连接上的写入读取延迟时,第一步应该是禁用 Nagle's algorithm通过这样做self.transport.setTcpNoDelay(True)在客户端和服务器的 connectionMade 方法中。

如果你想测量是你的网络连接问题还是Twisted问题,请仔细查看Wireshark在客户端和服务器上收集的日志并将它们与进程的日志进行比较应该可以让您了解流量的实际发送和接收时间以及应用程序层处理流量的时间。

如果您非常关心延迟,LoopingCall.withCount如果其他事情阻塞了主循环并导致计时器本身的测量不准确,将使您更好地了解。

关于python - 从 Twisted 服务器接收消息时出现延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30015006/

相关文章:

java - 如何在java中播放.wav文件而不出现游戏延迟

python - 如何在 Flask 中捕获和读取传入 HTTP 请求的 header ?

python - 在 python 中正确使用 NTLK

python - 为什么 SWIG 似乎会损坏成员类的内容?

python - 在函数内部导入 : is memory reclaimed upon function exit?

python - 有人可以解释这条扭曲的事吗?

python - 使用 Twisted 和 Comet 将命令结果流式传输回浏览器

python - 属性错误 : EchoFactory instance has no attribute 'doStart'

r - VAR 中的非连续滞后数(R 包 "vars")

jQuery 动画滞后和卡住