import logging import time from typing import Optional import grpc from models import GetCurrentTimeResponse from stubs import ( serve_hours_pb2, serve_hours_pb2_grpc, serve_minutes_pb2, serve_minutes_pb2_grpc, serve_seconds_pb2, serve_seconds_pb2_grpc, serve_milliseconds_pb2, serve_milliseconds_pb2_grpc, ) from stubs import serve_currenttime_pb2 from stubs import serve_currenttime_pb2_grpc SERVE_CURRENTTIME_HOST = "localhost" SERVE_CURRENTTIME_PORT = 50000 SERVE_HOURS_HOST = "localhost" SERVE_HOURS_PORT = 50001 SERVE_MINUTES_HOST = "localhost" SERVE_MINUTES_PORT = 50002 SERVE_SECONDS_HOST = "localhost" SERVE_SECONDS_PORT = 50003 SERVE_MILLISECONDS_HOST = "localhost" SERVE_MILLISECONDS_PORT = 50004 TIMEZONE = "Europe/Zagreb" def get_hours() -> Optional[int]: try: with grpc.insecure_channel(f"{SERVE_HOURS_HOST}:{SERVE_HOURS_PORT}") as channel: stub = serve_hours_pb2_grpc.ServeHoursStub(channel) response = stub.GetHours(serve_hours_pb2.GetHoursRequest(timezone=TIMEZONE)) return response.hours except Exception as ex: logging.error(ex) return None def get_minutes() -> Optional[int]: try: with grpc.insecure_channel( f"{SERVE_MINUTES_HOST}:{SERVE_MINUTES_PORT}" ) as channel: stub = serve_minutes_pb2_grpc.ServeMinutesStub(channel) response = stub.GetMinutes( serve_minutes_pb2.GetMinutesRequest(timezone=TIMEZONE) ) return response.minutes except Exception as ex: logging.error(ex) return None def get_seconds() -> Optional[int]: try: with grpc.insecure_channel( f"{SERVE_SECONDS_HOST}:{SERVE_SECONDS_PORT}" ) as channel: stub = serve_seconds_pb2_grpc.ServeSecondsStub(channel) response = stub.GetSeconds( serve_seconds_pb2.GetSecondsRequest(timezone=TIMEZONE) ) return response.seconds except Exception as ex: logging.error(ex) return None def get_milliseconds() -> Optional[int]: try: with grpc.insecure_channel( f"{SERVE_MILLISECONDS_HOST}:{SERVE_MILLISECONDS_PORT}" ) as channel: stub = serve_milliseconds_pb2_grpc.ServeMillisecondsStub(channel) response = stub.GetMilliseconds( serve_milliseconds_pb2.GetMillisecondsRequest(timezone=TIMEZONE) ) return response.milliseconds except Exception as ex: logging.error(ex) return None def get_currenttime() -> GetCurrentTimeResponse: with grpc.insecure_channel( f"{SERVE_CURRENTTIME_HOST}:{SERVE_CURRENTTIME_PORT}" ) as channel: stub = serve_currenttime_pb2_grpc.ServeCurrentTimeServiceStub(channel) raw_response = stub.GetCurrentTime( serve_currenttime_pb2.GetCurrentTimeRequest(timezone=TIMEZONE) ) return GetCurrentTimeResponse.from_orm(raw_response) def run(): t = time.perf_counter() hours = get_hours() minutes = get_minutes() seconds = get_seconds() milliseconds = get_milliseconds() t = time.perf_counter() - t # print(f"RESULT: {hours}:{minutes}:{seconds}:{milliseconds}") print(f"T: {t}") if __name__ == "__main__": logging.basicConfig() run()