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.9 kB

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

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