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 5.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 import log as logger
  20. from mindspore_serving._mindspore_serving import ExitSignalHandle_
  21. from mindspore_serving._mindspore_serving import Master_
  22. _wait_and_clear_thread = None
  23. # waiting for Ctrl+C, and clear
  24. def _start_wait_and_clear():
  25. """Start thread waiting for catch ctrl+c, and clear env"""
  26. def thread_func():
  27. logger.info("Serving master: wait for Ctrl+C to exit ------------------------------------")
  28. print("Serving master: wait for Ctrl+C to exit ------------------------------------")
  29. Master_.wait_and_clear()
  30. global _wait_and_clear_thread
  31. if not _wait_and_clear_thread:
  32. _wait_and_clear_thread = threading.Thread(target=thread_func)
  33. _wait_and_clear_thread.start()
  34. def stop():
  35. r"""
  36. Stop the running of master.
  37. Examples:
  38. >>> from mindspore_serving import master
  39. >>>
  40. >>> master.start_grpc_server("0.0.0.0", 5500)
  41. >>> master.start_restful_server("0.0.0.0", 1500)
  42. >>> ...
  43. >>> master.stop()
  44. """
  45. Master_.stop_and_clear()
  46. def stop_on_except(func):
  47. """Wrap of clear environment and exit on Serving exception"""
  48. @wraps(func)
  49. def handle_except(*args, **kwargs):
  50. try:
  51. ExitSignalHandle_.start() # Set flag to running and receive Ctrl+C message
  52. func(*args, **kwargs)
  53. except:
  54. stop()
  55. raise
  56. return handle_except
  57. @stop_on_except
  58. def start_grpc_server(ip="0.0.0.0", grpc_port=5500, max_msg_mb_size=100):
  59. r"""
  60. Start gRPC server for the communication between client and serving.
  61. Args:
  62. ip (str): gRPC server ip.
  63. grpc_port (int): gRPC port ip, default 5500, ip port range [1, 65535].
  64. max_msg_mb_size (int): The maximum acceptable gRPC message size in megabytes(MB), default 100,
  65. value range [1, 512].
  66. Raises:
  67. RuntimeError: Fail to start the gRPC server.
  68. Examples:
  69. >>> from mindspore_serving import master
  70. >>>
  71. >>> master.start_grpc_server("0.0.0.0", 5500)
  72. >>> master.start_restful_server("0.0.0.0", 1500)
  73. """
  74. check_type.check_str('ip', ip)
  75. check_type.check_ip_port('grpc_port', grpc_port)
  76. check_type.check_int('max_msg_mb_size', max_msg_mb_size, 1, 512)
  77. Master_.start_grpc_server(ip, grpc_port, max_msg_mb_size)
  78. _start_wait_and_clear()
  79. @stop_on_except
  80. def start_master_server(ip="127.0.0.1", master_port=6100):
  81. r"""
  82. Start the gRPC server for the communication between workers and the master.
  83. Note:
  84. The ip is expected to be accessed only by workers, not clients.
  85. Args:
  86. ip (str): gRPC ip for workers to communicate with, default '127.0.0.1'.
  87. master_port (int): gRPC port ip, default 6100, ip port range [1, 65535].
  88. Raises:
  89. RuntimeError: Fail to start the master server.
  90. Examples:
  91. >>> from mindspore_serving import master
  92. >>>
  93. >>> master.start_grpc_server("0.0.0.0", 5500)
  94. >>> master.start_restful_server("0.0.0.0", 1500)
  95. >>> master.start_master_server("127.0.0.1", 6100)
  96. """
  97. check_type.check_str('ip', ip)
  98. check_type.check_ip_port('master_port', master_port)
  99. Master_.start_grpc_master_server(ip, master_port)
  100. _start_wait_and_clear()
  101. @stop_on_except
  102. def start_restful_server(ip="0.0.0.0", restful_port=5900, max_msg_mb_size=100):
  103. r"""
  104. Start RESTful server for the communication between client and serving.
  105. Args:
  106. ip (str): RESTful server ip.
  107. restful_port (int): gRPC port ip, default 5900, ip port range [1, 65535].
  108. max_msg_mb_size (int): The maximum acceptable RESTful message size in megabytes(MB), default 100,
  109. value range [1, 512].
  110. Raises:
  111. RuntimeError: Fail to start the RESTful server.
  112. Examples:
  113. >>> from mindspore_serving import master
  114. >>>
  115. >>> master.start_restful_server("0.0.0.0", 1500)
  116. """
  117. check_type.check_str('ip', ip)
  118. check_type.check_ip_port('restful_port', restful_port)
  119. check_type.check_int('max_msg_mb_size', max_msg_mb_size, 1, 512)
  120. Master_.start_restful_server(ip, restful_port, max_msg_mb_size)
  121. _start_wait_and_clear()

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