2016-06-28 100 views
0

我在python中使用socketIO-client包來測試我的服務器代碼。 這是一個簡單的客戶端,但它總是需要大約1分鐘運行以下行:使用python socketIO-client包初始化socketIO速度太慢

socketIO = SocketIO('localhost',3003) 

我不知道如何解決這個問題呢?

我使用eventlet,並沒有在虛擬環境中安裝socketIO-client。 會導致這個問題嗎?

from socketIO_client import SocketIO, BaseNamespace 
from datetime import datetime 

Devices = {} 

temp_time = datetime.now() 

class Namespace(BaseNamespace): 
    def on_connect(self): 
     print('[Connected]') 
     for device in Devices: 
      self.emit('register', device) 
      time.sleep(1) 

def funcA(): 
    do something 

def funcB(): 
    do something 

print '1:', datetime.now()-temp_time 
socketIO = SocketIO('localhost', 3003) 
print '2:', datetime.now()-temp_time 
socketIO.define(Namespace) 

socketIO.on('eventA', funcA) 
socketIO.on('eventB', funcB) 
socketIO.wait() 

客戶端部分的輸出:

1: 0:00:00.000074 
DEBUG:root:localhost:3003/socket.io [transport selected] xhr-polling 
DEBUG:root:localhost:3003/socket.io [heartbeat reset] 
2: 0:01:00.019828 
DEBUG:root:localhost:3003/socket.io [socket.io packet received] 0 
[Connected] 

回答

-1

我的Python插座ioclient很快連接到外部的node.js服務器。 這可能是路由/ DNS問題 有時Localhost可能會出現奇怪的路由行爲。 使用127.0.0.1不localhost作爲URL來消除路由/ DNS超時