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.

_master.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  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. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """method of server supplied for master"""
  16. import threading
  17. from functools import wraps
  18. from mindspore_serving.worker import check_type
  19. from mindspore_serving._mindspore_serving import Master_
  20. _wait_and_clear_thread = None
  21. # waiting for Ctrl+C, and clear
  22. def _start_wait_and_clear():
  23. """Start thread waiting for catch ctrl+c, and clear env"""
  24. def thread_func():
  25. print("Serving master: wait for Ctrl+C to exit ------------------------------------")
  26. Master_.wait_and_clear()
  27. global _wait_and_clear_thread
  28. if not _wait_and_clear_thread:
  29. _wait_and_clear_thread = threading.Thread(target=thread_func)
  30. _wait_and_clear_thread.start()
  31. def stop():
  32. r"""
  33. Stop the running of master.
  34. Examples:
  35. >>> from mindspore_serving import master
  36. >>>
  37. >>> master.start_grpc_server("0.0.0.0", 5500)
  38. >>> master.start_restful_server("0.0.0.0", 1500)
  39. >>> ...
  40. >>> master.stop()
  41. """
  42. Master_.stop()
  43. def stop_on_except(func):
  44. """Wrap of clear environment and exit on Serving exception"""
  45. @wraps(func)
  46. def handle_except(*args, **kwargs):
  47. try:
  48. func(*args, **kwargs)
  49. except:
  50. stop()
  51. raise
  52. return handle_except
  53. @stop_on_except
  54. def start_grpc_server(ip="0.0.0.0", grpc_port=5500, max_msg_mb_size=100):
  55. r"""
  56. Start gRPC server for the communication between client and serving.
  57. Args:
  58. ip (str): gRPC server ip.
  59. grpc_port (int): gRPC port ip, default 5500,ip port range [0, 65535].
  60. max_msg_mb_size (int): The maximum acceptable gRPC message size in megabytes(MB), default 100,
  61. value range [1, 512].
  62. Raises:
  63. RuntimeError: Start gRPC server failed.
  64. Examples:
  65. >>> from mindspore_serving import master
  66. >>>
  67. >>> master.start_grpc_server("0.0.0.0", 5500)
  68. >>> master.start_restful_server("0.0.0.0", 1500)
  69. """
  70. check_type.check_str('ip', ip)
  71. check_type.check_ip_port('grpc_port', grpc_port)
  72. check_type.check_int('max_msg_mb_size', max_msg_mb_size, 1, 512)
  73. Master_.start_grpc_server(ip, grpc_port, max_msg_mb_size)
  74. _start_wait_and_clear()
  75. @stop_on_except
  76. def start_master_server(ip="127.0.0.1", master_port=6100):
  77. r"""
  78. Start gRPC server for the commication between workers and the master.
  79. Note:
  80. The ip is expected to be accessed only by workers, not clients.
  81. Args:
  82. ip (str): gRPC ip for worker to commnicate with, default '127.0.0.1'.
  83. master_port (int): gRPC port ip, default 6100,ip port range [0, 65535].
  84. Raises:
  85. RuntimeError: Start gRPC server failed.
  86. Examples:
  87. >>> from mindspore_serving import master
  88. >>>
  89. >>> master.start_grpc_server("0.0.0.0", 5500)
  90. >>> master.start_restful_server("0.0.0.0", 1500)
  91. >>> master.start_master_server("127.0.0.1", 6100)
  92. """
  93. check_type.check_str('ip', ip)
  94. check_type.check_ip_port('master_port', master_port)
  95. Master_.start_grpc_master_server(ip, master_port)
  96. _start_wait_and_clear()
  97. @stop_on_except
  98. def start_restful_server(ip="0.0.0.0", restful_port=5900, max_msg_mb_size=100):
  99. r"""
  100. Start RESTful server for the communication between client and serving.
  101. Args:
  102. ip (str): RESTful server ip.
  103. restful_port (int): gRPC port ip, default 5900,ip port range [0, 65535].
  104. max_msg_mb_size (int): The maximum acceptable RESTful message size in megabytes(MB), default 100,
  105. value range [1, 512].
  106. Raises:
  107. RuntimeError: Start RESTful server failed.
  108. Examples:
  109. >>> from mindspore_serving import master
  110. >>>
  111. >>> master.start_restful_server("0.0.0.0", 1500)
  112. """
  113. check_type.check_str('ip', ip)
  114. check_type.check_ip_port('restful_port', restful_port)
  115. check_type.check_int('max_msg_mb_size', max_msg_mb_size, 1, 512)
  116. Master_.start_restful_server(ip, restful_port, max_msg_mb_size)
  117. _start_wait_and_clear()

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.