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.

task.py 9.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. """Python run preprocess and postprocess in python"""
  16. import threading
  17. import time
  18. import logging
  19. from mindspore_serving._mindspore_serving import Worker_
  20. from mindspore_serving.worker.register.preprocess import preprocess_storage
  21. from mindspore_serving.worker.register.postprocess import postprocess_storage
  22. class ServingSystemException(Exception):
  23. def __init__(self, msg):
  24. super(ServingSystemException, self).__init__()
  25. self.msg = msg
  26. def __str__(self):
  27. return "Serving system error: " + self.msg
  28. task_type_stop = "stop"
  29. task_type_empty = "empty"
  30. task_type_preprocess = "preprocess"
  31. task_type_postprocess = "postprocess"
  32. class PyTask:
  33. def __init__(self, switch_batch, task_name):
  34. super(PyTask, self).__init__()
  35. self.task_name = task_name
  36. self.switch_batch = switch_batch
  37. self.temp_result = None
  38. self.task = None
  39. self.index = 0
  40. self.instances_size = 0
  41. self.stop_flag = False
  42. self.result_batch = []
  43. def push_failed_impl(self, count):
  44. raise NotImplementedError
  45. def push_result_batch_impl(self, result_batch):
  46. raise NotImplementedError
  47. def get_task_info(self, task_name):
  48. raise NotImplementedError
  49. def push_failed(self, count):
  50. self.push_result_batch() # push success first
  51. self.push_failed_impl(count)
  52. self.index += count
  53. def push_result_batch(self):
  54. if not self.result_batch:
  55. return
  56. get_result_time_end = time.time()
  57. last_index = self.index
  58. self.index += len(self.result_batch)
  59. self.push_result_batch_impl(tuple(self.result_batch))
  60. self.result_batch = []
  61. get_result_time = time.time()
  62. print(f"-----------------{self.task_name} push result {last_index} ~ {self.index - 1} cost time",
  63. (get_result_time - get_result_time_end) * 1000, "ms")
  64. def in_processing(self):
  65. return self.temp_result is not None
  66. def run(self, task=None):
  67. if not self.temp_result:
  68. assert task is not None
  69. self.instances_size = len(task.instance_list)
  70. self.index = 0
  71. self.task = task
  72. self.temp_result = self._handle_task()
  73. if not self.temp_result:
  74. return
  75. while self.index < self.instances_size:
  76. try:
  77. get_result_time_end = time.time()
  78. last_index = self.index
  79. for _ in range(self.index, min(self.index + self.switch_batch, self.instances_size)):
  80. output = next(self.temp_result)
  81. output = self._handle_result(output)
  82. self.result_batch.append(output)
  83. get_result_time = time.time()
  84. print(f"-----------------{self.task_name} get result {last_index} ~ cost time",
  85. (get_result_time - get_result_time_end) * 1000, "ms")
  86. self.push_result_batch()
  87. break
  88. except StopIteration:
  89. self.push_result_batch()
  90. self.push_failed(self.instances_size - self.index)
  91. raise RuntimeError(
  92. f"expecting '{self.task_name}' yield count equal to instance size {self.instances_size}")
  93. except ServingSystemException as e:
  94. raise e
  95. except Exception as e: # catch exception and try next
  96. print("{self.task_name} get result catch exception: ")
  97. logging.exception(e)
  98. self.push_failed(1) # push success results and a failed result
  99. self.temp_result = self._handle_task_continue()
  100. if self.index >= self.instances_size:
  101. self.temp_result = None
  102. def _handle_task(self):
  103. self.task_info = self.get_task_info(self.task.name)
  104. instance_list = self.task.instance_list
  105. self.context_list = self.task.context_list
  106. # check input
  107. for item in instance_list:
  108. if not isinstance(item, tuple) or len(item) != self.task_info["inputs_count"]:
  109. raise RuntimeError(f"length of given inputs {len(item)}"
  110. f" not match {self.task_name} required " + str(self.task_info["inputs_count"]))
  111. return self._handle_task_continue()
  112. def _handle_task_continue(self):
  113. if self.index >= self.instances_size:
  114. return None
  115. instance_list = self.task.instance_list
  116. try:
  117. outputs = self.task_info["fun"](instance_list[self.index:])
  118. return outputs
  119. except Exception as e:
  120. print(f"{self.task_name} invoke catch exception: ")
  121. logging.exception(e)
  122. self.push_failed(len(instance_list) - self.index)
  123. return None
  124. def _handle_result(self, output):
  125. if not isinstance(output, (tuple, list)):
  126. output = (output,)
  127. if len(output) != self.task_info["outputs_count"]:
  128. raise ServingSystemException(f"length of return output {len(output)} "
  129. f"not match {self.task_name} signatures " +
  130. str(self.task_info["outputs_count"]))
  131. output = (item.asnumpy() if callable(getattr(item, "asnumpy", None)) else item for item in output)
  132. return output
  133. class PyPreprocess(PyTask):
  134. def __init__(self, switch_batch):
  135. super(PyPreprocess, self).__init__(switch_batch, "preprocess")
  136. def push_failed_impl(self, count):
  137. Worker_.push_preprocess_failed(count)
  138. def push_result_batch_impl(self, result_batch):
  139. Worker_.push_preprocess_result(result_batch)
  140. def get_task_info(self, task_name):
  141. return preprocess_storage.get(task_name)
  142. class PyPostprocess(PyTask):
  143. def __init__(self, switch_batch):
  144. super(PyPostprocess, self).__init__(switch_batch, "postprocess")
  145. def push_failed_impl(self, count):
  146. Worker_.push_postprocess_failed(count)
  147. def push_result_batch_impl(self, result_batch):
  148. Worker_.push_postprocess_result(result_batch)
  149. def get_task_info(self, task_name):
  150. return postprocess_storage.get(task_name)
  151. class PyTaskThread(threading.Thread):
  152. def __init__(self, switch_batch):
  153. super(PyTaskThread, self).__init__()
  154. self.switch_batch = switch_batch
  155. if self.switch_batch <= 0:
  156. self.switch_batch = 8
  157. self.preprocess = PyPreprocess(self.switch_batch)
  158. self.postprocess = PyPostprocess(self.switch_batch)
  159. def run(self):
  160. print("start py task for preprocess and postprocess, switch_batch", self.switch_batch)
  161. preprocess_turn = True
  162. while True:
  163. try:
  164. if not self.preprocess.in_processing() and not self.postprocess.in_processing():
  165. task = Worker_.get_py_task()
  166. if task.task_type == task_type_stop:
  167. break
  168. if task.task_type == task_type_preprocess:
  169. self.preprocess.run(task)
  170. preprocess_turn = False
  171. elif task.task_type == task_type_postprocess:
  172. self.postprocess.run(task)
  173. preprocess_turn = True
  174. # in preprocess turn, when preprocess is still running, switch to running preprocess
  175. # otherwise try get next preprocess task when postprocess is running
  176. # when next preprocess is not available, switch to running postprocess
  177. if preprocess_turn:
  178. if self.preprocess.in_processing():
  179. self.preprocess.run()
  180. elif self.postprocess.in_processing():
  181. task = Worker_.try_get_preprocess_py_task()
  182. if task.task_type == task_type_stop:
  183. break
  184. if task.task_type != task_type_empty:
  185. self.preprocess.run(task)
  186. preprocess_turn = False
  187. else:
  188. if self.postprocess.in_processing():
  189. self.postprocess.run()
  190. elif self.preprocess.in_processing():
  191. task = Worker_.try_get_postprocess_py_task()
  192. if task.task_type == task_type_stop:
  193. break
  194. if task.task_type != task_type_empty:
  195. self.postprocess.run(task)
  196. preprocess_turn = True
  197. except Exception as e:
  198. print("py task catch exception and exit: ")
  199. logging.exception(e)
  200. break
  201. print("end py task for preprocess and postprocess")
  202. Worker_.stop()
  203. py_task_thread = None
  204. def start_py_task(switch_batch):
  205. global py_task_thread
  206. if py_task_thread is None:
  207. py_task_thread = PyTaskThread(switch_batch)
  208. py_task_thread.start()

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