[zeromq-dev] zeromq is asynchronous?

andrea crotti andrea.crotti.0 at gmail.com
Tue Aug 7 11:47:29 CEST 2012


Actually I'm very happy to see that this works exactly as expected, I
can communicate with a process asking querying the status, still open
for other suggestions..

__metaclass__ = type
import sys
import zmq

from time import sleep

PORT = 'tcp://*:5554'

from threading import Thread


class Monitor(Thread):
    def __init__(self, proc):
        Thread.__init__(self)
        self.proc = proc
        context = zmq.Context()
        self.socket = context.socket(zmq.REP)
        self.socket.bind(PORT)

    def run(self):
        while True:
             query = self.socket.recv()
             self.socket.send(str(self.proc.status))


class Proc:
    def __init__(self):
        self.status = 0

    def run(self):
        while True:
            self.status += 1
            sleep(2)


def start_proc_and_monitor():
    pr = Proc()
    thr = Monitor(pr)
    print("starting the monitor thread")
    thr.start()
    #only start in the end
    pr.run()


def start_query():
    context = zmq.Context()
    socket = context.socket(zmq.REQ)
    socket.connect(PORT)

    while True:
        raw_input("ask for status?")
        socket.send('status')
        ans = socket.recv()
        print("status now = ", ans)


if __name__ == '__main__':
    if len(sys.argv) > 1:
        start_proc_and_monitor()
    else:
        start_query()



More information about the zeromq-dev mailing list