2

我创建了一个 Web 套接字服务器,ws4py它将消息分派到 ZMQ 消息总线并将响应返回到 Web 套接字。精简后的代码如下所示:

from  multiprocessing import Process
from gevent import monkey; monkey.patch_all()

from ws4py.websocket import WebSocket
from ws4py.server.geventserver import WSGIServer
from ws4py.server.wsgiutils import WebSocketWSGIApplication

import time

import gevent
from zmq.green.eventloop import ioloop, zmqstream
import zmq.green as zmq


class MyWebSocket(WebSocket):

    def __init__(self, *args, **kwargs):
        super(MyWebSocket, self).__init__(*args, **kwargs)

        uri = 'tcp://127.0.0.1:5560'
        context = zmq.Context()

        self.publisher = context.socket(zmq.PUB)
        self.publisher.bind(uri)

        subscriber = context.socket(zmq.SUB)
        subscriber.connect(uri)
        subscriber.setsockopt(zmq.SUBSCRIBE, '')

        loop = ioloop.IOLoop()
        zstream = zmqstream.ZMQStream(subscriber, loop)

        def _recv_result(msg):
            print 'received from publisher', msg

        zstream.on_recv(_recv_result)
        gevent.spawn(loop.start)

    def received_message(self, message):
        # incoming from web client.
        data = message.data
        self.publisher.send_multipart(['id', data])


if __name__ == '__main__':
    host, port = 'localhost', 9000
    server = WSGIServer(
        (host, port), WebSocketWSGIApplication(handler_cls=MyWebSocket)
    )
    server.serve_forever()

在这个最小的示例中,我删除了将结果发送回消息总线并直接订阅消息的系统处理部分(本质上使它成为一个复杂的回显服务器)。问题是_recv_result()永远不会被调用。我删除了所有订阅过滤器前缀,并将其设为gevent线程,但这无济于事。有谁知道可能出了什么问题?

4

0 回答 0