2011-05-16 40 views
0

id喜歡爲使用線程的pywebsocket(http://code.google.com/p/pywebsocket)提供的獨立服務器編寫處理程序。 與pywebsocket來處理程序的例子僅僅是一個函數文件:使用線程的python HTTPServer的處理程序

def web_socket_transfer_data(request): 
    while True: 
    line = request.ws_stream.receive_message() 
    if line is None: 
     return 
    request.ws_stream.send_message(line) 
    if line == _GOODBYE_MESSAGE: 
     return 

香港專業教育學院嘗試添加一個線程:

class _Stub(threading.Thread): 
def __init__ (self): 
    threading.Thread.__init__(self) 
    self._test = 0 

def run(self): 
    while True: 
     time.sleep(5) 
     self._test = self._test + 1 

但服務器崩潰,沒有任何評論... 所以這是如何完成的?

感謝任何指針。

回答

0

獨立服務器不旨在接收非阻塞消息。從msgutil.py(至少不使用SSL時)的類「MessageReceiver」的文檔:

該類接收來自客戶端的消息。

This class provides three ways to receive messages: blocking, 
non-blocking, and via callback. Callback has the highest precedence. 

Note: This class should not be used with the standalone server for wss 
because pyOpenSSL used by the server raises a fatal error if the socket 
is accessed from multiple threads.