Exemplo n.º 1
0
def main():
    verbose = '-v' in sys.argv
    ctx = zmq.Context()

    # Create MDP client session with short timeout
    client = MajorDomoClient("tcp://localhost:5555", verbose)
    client.timeout = 1000 # 1 sec
    client.retries = 1 # only 1 retry

    request_pipe, peer = zpipe(ctx)
    request_thread = threading.Thread(target=titanic_request, args=(peer,))
    request_thread.daemon = True
    request_thread.start()
    reply_thread = threading.Thread(target=titanic_reply)
    reply_thread.daemon = True
    reply_thread.start()
    close_thread = threading.Thread(target=titanic_close)
    close_thread.daemon = True
    close_thread.start()

    poller = zmq.Poller()
    poller.register(request_pipe, zmq.POLLIN)

    # Main dispatcher loop
    while True:
        # Ensure message directory exists
        if not os.path.exists(TITANIC_DIR):
            os.mkdir(TITANIC_DIR)
        # We'll dispatch once per second, if there's no activity
        try:
            items = poller.poll(1000)
        except KeyboardInterrupt:
            break

        if items:
            # Append UUID to queue, prefixed with '-' for pending
            uuid = request_pipe.recv()
            with open(os.path.join(TITANIC_DIR, 'queue'), 'a') as f:
                f.write("-%s\n" % uuid)

        # Brute force dispatcher
        with open(os.path.join(TITANIC_DIR, 'queue'), 'r+b') as f:
            for entry in f.readlines():
                # UUID is prefixed with '-' if still waiting
                if entry[0] == '-':
                    uuid = entry[1:].rstrip() # rstrip '\n' etc.
                    print("I: processing request %s" % uuid)
                    if service_success(client, uuid):
                        # mark queue entry as processed
                        here = f.tell()
                        f.seek(-1*len(entry), os.SEEK_CUR)
                        f.write("+")
                        f.seek(here, os.SEEK_SET)
Exemplo n.º 2
0
    def test_zpipe(self, ctx):
        """Test zpipe method

        Setting up two inproc PAIRs for threading
        """
        s1, s2 = zpipe(ctx)

        # check high water mark setting
        assert s1.get_hwm() == 1
        assert s2.get_hwm() == 1

        # check linger setting
        assert s1.get(zmq.LINGER) == 0
        assert s2.get(zmq.LINGER) == 0

        # check sending message between pair
        msg = [b'', b'hello']
        s1.send_multipart(msg)
        resp = s2.recv_multipart()
        assert msg == resp