service.py 1.0 KB

123456789101112131415161718192021222324252627282930
  1. import logging
  2. import threading
  3. from easyfl.pb import client_service_pb2_grpc as client_grpc, client_service_pb2 as client_pb, common_pb2 as common_pb
  4. from easyfl.protocol import codec
  5. logger = logging.getLogger(__name__)
  6. class ClientService(client_grpc.ClientServiceServicer):
  7. """"Remote gRPC client service.
  8. Args:
  9. client (:obj:`BaseClient`): Federated learning client instance.
  10. """
  11. def __init__(self, client):
  12. self._base = client
  13. def Operate(self, request, context):
  14. """Perform training/testing operations."""
  15. # TODO: add request validation.
  16. model = codec.unmarshal(request.model)
  17. is_train = request.type == client_pb.OP_TYPE_TRAIN
  18. # Threading is necessary to respond to server quickly
  19. t = threading.Thread(target=self._base.operate, args=[model, request.config, request.data_index, is_train])
  20. t.start()
  21. response = client_pb.OperateResponse(
  22. status=common_pb.Status(code=common_pb.SC_OK),
  23. )
  24. return response