2013-08-16 42 views
3

我在Linux中有一個簡單的Asyncore客戶端與在Windows機器上用C開發的服務器交互。蟒蛇asyncore跟不上高數據速率

該設置適用於寫入asyncore緩衝區的數據速率大約爲每秒一次,但每當我的數據速率超過該速度時(我已經每0.1秒測試一次),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="" 
+0

取而代之的將答案在問題結束時,你應該添加一個答案並接受它(是的,這是合法的接受你自己的答案。) – Bakuriu

+0

感謝您的提示。我無法立即發佈答案,只是因爲我必須等待至少八個小時才能回答自己的問題。 –

回答

2

我找到了這個問題的答案。在這種情況下Asyncore不應該被指責。問題中提供的代碼對緩衝區接口的使用不當。我所要做的就是替換handle_write方法與以下行的最後一行:

self.buffer = self.buffer [發送:]

這解決了問題