zeromq:为什么经销商发送一条消息并从一个客户端收到?

时间:2014-02-12 12:32:42

标签: python python-2.7 zeromq pyzmq

我正在尝试理解代码的行为。 我正在使用zeromq创建一个发送“ping”并等待“pong”响应的服务器。 我所看到的是,当我发送ping时,只有一个客户端收到它。 当我运行此代码并第一次发送“ping”时,我收到:

pong: A

当我再次运行时,我得到了

pong: B

为什么?我想发一个“ping”并收到两个pongs。

这是代码:

from threading import Thread
import zmq

class zmqdealer(object):
    def __init__(self, port):
        context = zmq.Context()
        self.sock = context.socket(zmq.DEALER)
        #self.sock.setsockopt(zmq.RCVTIMEO, 1000)
        self.sock.bind("tcp://*:%s" % port)
        thread = Thread(target=lambda: self.poll())
        thread.daemon = True
        thread.start()

    def poll(self):
        while True:
            reply = self.sock.recv()
            if reply != "":
                print(reply)

    def ping(self):
        self.sock.send_multipart(['', 'ping'])


class zmqrep(object):
    def __init__(self, ident,host, port):
        context = zmq.Context()
        self.sock = context.socket(zmq.REP)
        self.sock.connect("tcp://%s:%s" % (host, port))
        self.ident = ident
        thread = Thread(target=lambda: self.pong())
        thread.daemon = True
        thread.start()

    def pong(self):
        while True:
            request = self.sock.recv()
            if request == "ping":
                msg = "pong: %s" % self.ident
                self.sock.send(msg)


if __name__ == "__main__":
    port = 11112
    host = "localhost"
    server = zmqdealer(port)
    client1 = zmqrep('A',host,port)
    client2 = zmqrep('B',host,port)
    answer = raw_input('press <ENTER> to exit or type \'ping\' to get a pong\n')
    while True:
        if answer == "":
            break
        if answer == "ping":
            server.ping()
        answer = raw_input()

修改

我找到了一种方法来完成这项工作。我真的希望有另一种方式,因为我真的很讨厌这个!所以看起来经销商以循环方式发送给客户。为了使我的ping工作,我必须将它发送给所有客户端。怎么样?我订阅了监视器套接字并将每个连接的客户端添加到列表中。每次我ping,我都会ping每个客户端。看:

import threading
import zmq
from zmq.utils import monitor

def threadify(func, daemon=True):
    thread = threading.Thread(target=func)
    thread.daemon = daemon
    thread.start()

class zmqdealer(object):

    def __init__(self, port):
        context = zmq.Context()
        self.sock = context.socket(zmq.DEALER)
        self.monitor_sock = self.sock.get_monitor_socket()
        self.sock.bind("tcp://*:%s" % port)
        self.connected_clients = {}
        threadify(func=self.poll)
        threadify(func=self.monitor)

    def poll(self):
        while True:
            reply = self.sock.recv()
            if reply != "":
                print reply


    def add_client(self, event):
        endpoint = event['endpoint']
        value = event['value']
        if endpoint in self.connected_clients:
            self.connected_clients[endpoint].append(value)
        else:
            self.connected_clients[endpoint] = [value]

    def remove_client(self, event):
        endpoint = event['endpoint']
        value = event['value']
        if endpoint in self.connected_clients \
                and value in self.connected_clients[endpoint]:
            self.connected_clients[endpoint].remove(value)

    def monitor(self):
        options = {zmq.EVENT_ACCEPTED: lambda e: self.add_client(e),
                   zmq.EVENT_DISCONNECTED: lambda e: self.remove_client(e)}
        while True:
            event = monitor.recv_monitor_message(self.monitor_sock)
            event_type = event['event']
            if event_type in options:
                options[event_type](event)

            event['event'] = event_types[event_type]
            print event

    def ping(self):
        connected_clients_amount = sum([len(clients) for clients in self.connected_clients.values()])
        for i in xrange(connected_clients_amount):
            self.sock.send_multipart(['', 'ping'])

        if connected_clients_amount <= 0:
            print "there are no connected clients!"


class zmqrep(object):
    def __init__(self, ident, host, port):
        context = zmq.Context()
        self.sock = context.socket(zmq.REP)
        self.sock.connect("tcp://%s:%s" % (host, port))
        self.identity = ident
        self.stopped = threading.Event()
        threadify(self.pong)

    def pong(self):
        while not self.stopped.isSet():
            request = self.sock.recv()
            if request == "ping":
                msg = "pong: %s" % self.identity
                self.sock.send(msg)
        self.sock.close()

    def stop(self):

        self.stopped.set()


if __name__ == "__main__":
    port = 11112
    host = "localhost"
    num = 5
    server = zmqdealer(port)
    clients = [zmqrep(i.__str__(), host, port) for i in xrange(num)]
    answer = raw_input('press <ENTER> to exit or type \'ping\' to get a pong\n')
    while True:
        if answer == "":
            break
        if answer == "ping":
            server.ping()
        if answer == "kill":
            if len(clients) > 0:
                die = clients[0]
                clients.remove(die)
                die.stop()
            else:
                print "there are no connected clients!\n"

        answer = raw_input()

1 个答案:

答案 0 :(得分:1)

路由器/经销商套接字最适合用于分发任务。假设你有10个任务和2个工人,你不关心谁做了什么。经销商/路由器将以循环方式分发。

也许Pub / Sub或Push / Pull套接字更适合您的用例?它们都是广播插座。

这是推送/拉动的example,其使用方式与您正在进行的操作类似。

你经常会做一对套接字,一个用于传输,另一个用于接收结果。例如,您可以使用ping消息+随机标识符执行PUSH,并要求客户端在PUB / SUB上回答您订阅此随机标识符的位置。这样您就可以匹配请求和响应。