Question

I am looking to use the Majordomo API, pyzmq-mdp. The example in the repository for the client, is something I don't quite understand.

In the myclient.py, the example creates a socket manually without using the MDPClient API. And to make matters worse the mdp_request method is used for making a request. But I tried this so far:

class Client(MDPClient):

    def on_timeout(self):
        print "Client Timed out."

def main():
    context = zmq.Context()
    mdp_client = Client(context, "tcp://127.0.0.1:8888", SERVICE)
    try:
        mdp_client.request(["hello"], timeout=5000)
    except RequestTimeout:
        print "Timed Out."


if __name__ == '__main__':
    main()

I am solely running the client. No broker. But this is not raising the RequestTimeout exception. I have the following doubts:

  • Why isn't the example implementing the MDPClient API?
  • What is the purpose of mdp_request method when there is a request method available?
  • What is wrong with my approach?

Am I missing something obvious? Some guidance please.

Was it helpful?

Solution

According to your questions:

What is the purpose of mdp_request method when there is a request method available?

as it's written in code:

mdp_request is synchronous MDP request

MDPClient class is asynchronous encapsulation of a zmq.REQ socket and is meant to be integrated into the asynchronous IOLoop of pyzmq.

What is wrong with my approach?

I was struggling with the same problem. After checking the test_client.py I was able to solve it with such example:

import zmq
from zmq.eventloop.ioloop import IOLoop
from mdp.client import MDPClient

class MyClient(MDPClient):
    def on_message(self, msg):
        print("Received:", repr(msg))
        IOLoop.instance().stop()
        return

    def on_timeout(self):
        print('TIMEOUT!')
        IOLoop.instance().stop()
        return

if __name__ == '__main__':
    context = zmq.Context()
    client = MyClient(context, 'tcp://127.0.0.1:5555', b'echo')
    client.request(b'Hello world', timeout=3000)
    IOLoop.instance().start()
    client.shutdown()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top