def deserialize_and_reply(self, request): start_working = datetime.datetime.now() if not ZMQUtils.is_valid_envelop(request): print 'Invalid Request:', TypeError res = error_response(ERROR_INVALID_ENVELOP) else: if self.verbose: print "raw_request", request pboreq = PBOneRequest() res = '' try: pboreq = PBOneRequest() pboreq.ParseFromString(request[0]) pboresp = self.handle_request(pboreq) end_working = datetime.datetime.now() delta = end_working - start_working pboresp.gresp.computetime = delta.total_seconds() res = pboresp.SerializeToString() except TypeError: print 'Exception:', TypeError traceback.print_exc() res = error_response(ERROR_PARSING_EXCEPTION) except Exception: print 'General Exception:', Exception traceback.print_exc() res = error_response(ERROR_PARSING_EXCEPTION) finally: if self.verbose: print 'raw_reply:', res return [res]
def main(): ############################################## # ARGUMENTS PARSING ############################################## parser = argparse.ArgumentParser(description='Send requests') parser.add_argument('numreq', help='number of request to send', type=int) parser.add_argument( '-c', '--content', help='content data to send to simple request', type=str) parser.add_argument( '-v', '--verbose', help='increase output verbosity', action='store_true') parser.add_argument( '-a', '--asynchronous', help='send/receive asynchronous', action='store_true') parser.set_defaults(numreq=10) parser.set_defaults(content='youpi') args = parser.parse_args() print "Program Launched with args:" + str(args) print "Number of requests:" + str(args.numreq) print "Verbose:" + str(args.verbose) print "Content:" + str(args.content) data = args.content verbose = args.verbose NB_REQUEST = args.numreq start_launcher = datetime.datetime.now() client = MajorDomoClient("tcp://localhost:5555", verbose) requests = NB_REQUEST count = 0 if args.asynchronous: for _ in xrange(requests): try: request = simple_request(data) client.send("echo", request) if verbose: print 'request sended:', request except KeyboardInterrupt: print "send interrupted, aborting" return while count < requests: try: reply = client.recv() except KeyboardInterrupt: break else: # also break on failure to reply: if reply is None: break else: # check if my reponse is a list of one if ZMQUtils.is_valid_envelop(reply): # then sresp = PBOneResponse() sresp.ParseFromString(reply[0]) handle_response(sresp) else: print "received invalid response envelop" count += 1 else: for _ in xrange(requests): try: request = simple_request(data) client.send("echo", request) if verbose: print 'request sended:', request reply = client.recv() if reply is None: print 'None reply' else: # check if my reponse is a list of one if ZMQUtils.is_valid_envelop(reply): # then sresp = PBOneResponse() sresp.ParseFromString(reply[0]) handle_response(sresp) else: print "received invalid response envelop" except KeyboardInterrupt: print "send interrupted, aborting" return count += 1 print "%i requests/replies processed" % count end_launcher = datetime.datetime.now() delta = end_launcher - start_launcher print 'Launcher ALL JOBS DONE in ', str(delta), ' !!!!' print 'Avergage round-trip:', str(delta / requests)