繁体   English   中英

python asyncore无法跟上高数据速率

[英]python asyncore not keeping up with high data rates

我在Linux中有一个简单的Asyncore客户端与在Windows机器上用C开发的服务器交互。

该设置适用于大约每秒一次写入asyncore缓冲区的数据速率,但每当我的数据速率超过(我每0.1秒测试一次)时,Asyncore不会发送所有写入。 某些缓冲区值丢失,永远不会进入服务器。

我怀疑这是因为asyncore经历了一个循环并且只检查缓冲区中是否有数据每个循环发送一次,如果数据速率高于循环时间,那么它们就会丢失。 这是真的? 如果是这样,是否有一个解决方法到这个或不同于Asyncore的不同套接字模块不会造成这个问题?

这是我正在使用的代码:

class DETClient(asyncore.dispatcher):

    buffer = ""
    t = None

    def __init__(self, host, port):

        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host,port))
        self.host=host
        self.port=port
        self.t = ReceiverBoard(self)
        self.t.start()

    def initiate_connection_with_server(self):
        print("trying to initialize connection with server...")
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((self.host,self.port))

    def sendCommand(self, command):
        self.buffer = command +'\0'

    def handle_connect(self):
        pass

    def handle_error(self):
        print("problem reaching server.")
        self.initiate_connection_with_server()

    def handle_close(self):
        self.t.stop()
        self.close()

    def handle_read(self):
        message = str(self.recv(20)) #how many bytes should be read?

        if (message=="b'system_bias_ON'"):
            self.t.enable_sys_bias()

        if (message=="b'system_bias_OFF'"):
            self.t.disable_sys_bias()

        if (message[2:5]=="DET"):
            self.t.DET_bias_command_received = True
            self.t.DET_bias_command_text = message

        if (message[2:7]=="CURVE"):
            self.t.DET_bias_command_received = True
            self.t.DET_bias_command_text = message

    def handle_write(self):
        sent=self.send(self.buffer.encode())
        self.buffer=""

我找到了这个问题的答案。 在这种情况下,Asyncore不应该受到指责。 问题中提供的代码是错误地使用缓冲区接口。 我所要做的就是用以下行替换handle_write方法中的最后一行:

self.buffer = self.buffer [sent:]

这解决了这个问题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM