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) queue_filename = os.path.join(TITANIC_DIR, 'queue') # Main dispatcher loop while True: # Ensure message directory exists if not os.path.exists(TITANIC_DIR): os.mkdir(TITANIC_DIR) f = open(queue_filename, 'wb') f.close() # We'll dispatch once per second, if there's no activity try: items = poller.poll(1000) except KeyboardInterrupt: break # Interrupted if items: # Append UUID to queue, prefixed with '-' for pending suuid = request_pipe.recv().decode('utf-8') with open(queue_filename, 'ab') as f: line = "-%s\n" % suuid f.write(line.encode('utf-8')) # Brute-force dispatcher with open(queue_filename, 'rb+') as f: for entry in f.readlines(): entry = entry.decode('utf-8') # UUID is prefixed with '-' if still waiting if entry[0] == '-': suuid = entry[1:].rstrip() # rstrip '\n' etc. print("I: processing request %s" % suuid) if service_success(client, suuid): # mark queue entry as processed here = f.tell() f.seek(-1 * len(entry), os.SEEK_CUR) f.write('+'.encode('utf-8')) f.seek(here, os.SEEK_SET)
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 # Interrupted 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)
def main(): verbose = '-v' in sys.argv ctx = zmq.Context() # Create MDP client session with short timeout # this client is used by service_success method 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, verbose, )) request_thread.daemon = True request_thread.start() reply_thread = threading.Thread(target=titanic_reply, args=(verbose, )) reply_thread.daemon = True reply_thread.start() close_thread = threading.Thread(target=titanic_close, args=(verbose, )) close_thread.daemon = True close_thread.start() poller = zmq.Poller() poller.register(request_pipe, zmq.POLLIN) # Ensure message directory exists TITANIC_DIR.mkdir(parents=True, exist_ok=True) # create the dispatcher queue file, if not present queue = TITANIC_DIR.joinpath('queue') queue.touch() # Main dispatcher loop while True: # We'll dispatch once per second, if there's no activity try: items = poller.poll(1000) except KeyboardInterrupt: break # Interrupted if items: # Append UUID to queue, prefixed with '-' for pending uuid = request_pipe.recv() with open(queue, 'a') as f: f.write(f"-{uuid.decode()}\n") # Brute-force dispatcher with open(queue, 'r+b') as f: for entry in f.readlines(): entry = entry.decode() # UUID is prefixed with '-' if still waiting if entry[0] == '-': uuid = entry[1:].rstrip() # rstrip '\n' etc. print(f"I: processing request {uuid}") if service_success(client, uuid): # mark queue entry as processed here = f.tell() f.seek(-1 * len(entry), os.SEEK_CUR) f.write(b'+') f.seek(here, os.SEEK_SET) print(f"completed {uuid}")