Skip to content

Instantly share code, notes, and snippets.

@rakia
Created April 11, 2024 13:31
Show Gist options
  • Save rakia/3acdd26c138da0b1dc223b6acb4204f6 to your computer and use it in GitHub Desktop.
Save rakia/3acdd26c138da0b1dc223b6acb4204f6 to your computer and use it in GitHub Desktop.
Server-side logic of GetWeather ProtoBuf (gRPC) service
from concurrent import futures
import grpc
import weather_pb2
import weather_pb2_grpc
class WeatherServicer(weather_pb2_grpc.WeatherServiceServicer):
def GetWeather(self, request, context):
# Implement the logic to retrieve the weather here. For simplicity, returning a fixed value.
return weather_pb2.GetWeatherResponse(temperature=22.5)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
weather_pb2_grpc.add_WeatherServiceServicer_to_server(WeatherServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
server.wait_for_termination()
if __name__ == '__main__':
serve()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment