You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

grpc_server.py 3.7 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. """
  13. import grpc
  14. import time
  15. import json
  16. import config as configs
  17. from logger import Logger
  18. from utils import file_utils
  19. from concurrent import futures
  20. from proto import inference_pb2_grpc, inference_pb2
  21. from service.inference_service_manager import InferenceServiceManager
  22. from response import Response
  23. log = Logger().logger
  24. parser = configs.get_parser()
  25. args = parser.parse_args()
  26. inference_service = InferenceServiceManager(args)
  27. inference_service.init()
  28. MAX_MESSAGE_LENGTH = 1024 * 1024 * 1024 # 可根据具体需求设置,此处设为1G
  29. def response_convert(response):
  30. """
  31. 返回值封装字典
  32. """
  33. return {
  34. 'success': response.success,
  35. 'data': response.data,
  36. 'error': response.error
  37. }
  38. class InferenceService(inference_pb2_grpc.InferenceServiceServicer):
  39. """
  40. 调用grpc方法进行推理
  41. """
  42. def inference(self, request, context):
  43. data_list = request.data_list
  44. log.info("===============> grpc inference start <===============")
  45. try:
  46. data_list_b64 = file_utils.upload_image_by_base64(data_list) # 上传图片到本地
  47. except Exception as e:
  48. log.error("upload data failed", e)
  49. return inference_pb2.DataResponse(json_result=json.dumps(
  50. response_convert(Response(success=False, data=str(e), error="upload data failed"))))
  51. try:
  52. result = inference_service.inference(args.model_name, data_list_b64)
  53. log.info("===============> grpc inference success <===============")
  54. return inference_pb2.DataResponse(json_result=json.dumps(
  55. response_convert(Response(success=True, data=result))))
  56. except Exception as e:
  57. log.error("inference fail", e)
  58. return inference_pb2.DataResponse(json_result=json.dumps(
  59. response_convert(Response(success=False, data=str(e), error="inference fail"))))
  60. def main():
  61. server = grpc.server(futures.ThreadPoolExecutor(max_workers=10), options=[
  62. ('grpc.max_send_message_length', MAX_MESSAGE_LENGTH),
  63. ('grpc.max_receive_message_length', MAX_MESSAGE_LENGTH),
  64. ])
  65. inference_pb2_grpc.add_InferenceServiceServicer_to_server(InferenceService(), server)
  66. if args.enable_tls: # 使用tls加密通信
  67. with open(args.secret_key, 'rb') as f:
  68. private_key = f.read()
  69. with open(args.secret_crt, 'rb') as f:
  70. certificate_chain = f.read()
  71. # create server credentials
  72. server_credentials = grpc.ssl_server_credentials(((private_key, certificate_chain,),))
  73. server.add_secure_port(args.host + ':' + str(args.port), server_credentials) # 添加监听端口
  74. else: # 不使用tls加密通信
  75. server.add_insecure_port(args.host + ':' + str(args.port)) # 添加监听端口
  76. server.start() # 启动服务器
  77. try:
  78. while True:
  79. log.info('===============> grpc server start <===============')
  80. time.sleep(60 * 60 * 24)
  81. except KeyboardInterrupt:
  82. server.stop(0) # 关闭服务器
  83. if __name__ == '__main__':
  84. main()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能