0

我喜欢为使用线程的 pywebsocket (http://code.google.com/p/pywebsocket) 附带的独立服务器编写一个处理程序。在 pywebsocket 附带的示例中,处理程序只是一个带有函数的文件:

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

但是服务器在没有任何评论的情况下崩溃了......那么这是怎么做到的呢?

感谢您的任何指点。

4

1 回答 1

0

独立服务器并非旨在接收非阻塞消息。从msgutil.py中“MessageReceiver”类的文档中(至少在使用 SSL 时不是):

此类接收来自客户端的消息。

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.
于 2012-04-16T14:12:10.527 回答