process_manager.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import socket
  5. import random
  6. import zerorpc
  7. import cv2
  8. import msgpack
  9. import numpy as np
  10. import io
  11. from typing import Union, Any
  12. from cached_property import cached_property
  13. from PySide6.QtCore import QObject, Slot, Signal, Property
  14. from PySide6.QtGui import QImage
  15. from module.gui.process.script_process import ScriptProcess
  16. from module.config.config_menu import ConfigMenu
  17. from module.gui.context.add import Add
  18. from module.logger import logger
  19. def is_port_in_use(ip, port) -> bool:
  20. """
  21. 检查端口是否被占用
  22. :param ip:
  23. :param port:
  24. :return:
  25. """
  26. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27. try:
  28. s.connect((ip, port))
  29. s.shutdown(2)
  30. logger.info(f'port {port} is in use')
  31. return True
  32. except:
  33. logger.info(f'port {port} is not in use')
  34. return False
  35. class ProcessManager(QObject):
  36. """
  37. 进程管理
  38. """
  39. def __init__(self) -> None:
  40. """
  41. init
  42. """
  43. super().__init__()
  44. self.processes = {} # 持有所有的进程
  45. self.ports = {} # 持有所有的端口
  46. self.clients = {} # zerorpc连接的客户端
  47. @Slot()
  48. def create_all(self) -> None:
  49. """
  50. 创建所有的配置实例
  51. :return:
  52. """
  53. configs = Add().all_script_files()
  54. for config in configs:
  55. self.add(config)
  56. def add(self, config: str) -> None:
  57. """
  58. add
  59. :param config:
  60. :return:
  61. """
  62. if config not in self.processes:
  63. port = 40000 + random.randint(0, 200)
  64. while is_port_in_use('127.0.0.1', port):
  65. port = 40000 + random.randint(0, 200)
  66. self.ports[config] = port
  67. self.processes[config] = ScriptProcess(config, port)
  68. logger.info(f'create script {config} on port {port}')
  69. try:
  70. self.clients[config] = zerorpc.Client()
  71. self.clients[config].connect(f'tcp://127.0.0.1:{self.ports[config]}')
  72. except:
  73. logger.exception(f'connect to script {config} error')
  74. raise
  75. self.processes[config].start()
  76. logger.info(f'add script {config}')
  77. else:
  78. logger.info(f'script {config} is already running')
  79. def remove(self, config: str) -> None:
  80. """
  81. remove
  82. :param config:
  83. :return:
  84. """
  85. if config in self.processes:
  86. self.processes[config].stop()
  87. del self.processes[config]
  88. del self.ports[config]
  89. del self.clients[config]
  90. logger.info(f'remove script {config}')
  91. logger.info(f'port {config} is released')
  92. else:
  93. logger.info(f'script {config} is not running')
  94. def restart(self, config: str) -> None:
  95. """
  96. restart
  97. :param config:
  98. :return:
  99. """
  100. if config in self.processes:
  101. self.processes[config].restart()
  102. logger.info(f'restart script {config}')
  103. else:
  104. logger.info(f'script {config} is not running')
  105. def stop_all(self) -> None:
  106. """
  107. stop_all
  108. :return:
  109. """
  110. for config in self.processes:
  111. self.processes[config].stop()
  112. logger.info(f'stop all script')
  113. def restart_all(self) -> None:
  114. """
  115. restart_all
  116. :return:
  117. """
  118. for config in self.processes:
  119. self.processes[config].restart()
  120. logger.info(f'restart all script')
  121. def get_client(self, config: str) -> zerorpc.Client:
  122. """
  123. get_client
  124. :param config:
  125. :return:
  126. """
  127. if config in self.clients:
  128. return self.clients[config]
  129. else:
  130. logger.info(f'script {config} is not running')
  131. return None
  132. @Slot(result="QString")
  133. def gui_menu(self) -> str:
  134. """
  135. get_gui_menu
  136. :param config:
  137. :return:
  138. """
  139. menu = ConfigMenu()
  140. return menu.gui_menu
  141. @Slot(str, str, result="QString")
  142. def gui_args(self, config: str, task: str) -> str:
  143. """
  144. 获取显示task的gui参数
  145. :param config:
  146. :param task:
  147. :return:
  148. """
  149. if config in self.clients:
  150. logger.info(f'gui get args of {config} {task}')
  151. return self.clients[config].gui_args(task)
  152. else:
  153. logger.info(f'script {config} is not running')
  154. return None
  155. @Slot(str, str, result="QString")
  156. def gui_task(self, config: str, task: str) -> str:
  157. """
  158. 获取显示task的gui
  159. :param config:
  160. :param task:
  161. :return:
  162. """
  163. if config in self.clients:
  164. logger.info(f'gui get value of {config} {task}')
  165. return self.clients[config].gui_task(task)
  166. else:
  167. logger.info(f'script {config} is not running')
  168. return None
  169. @Slot(str, str, str, str, str, result="bool")
  170. def gui_set_task(self, config: str, task: str, group: str, arg: str, value) -> bool:
  171. """
  172. 设置task的gui 是string类型的
  173. :param config:
  174. :param task:
  175. :param group:
  176. :param arg:
  177. :param value:
  178. :return:
  179. """
  180. if config in self.clients:
  181. logger.info(f'gui set value of {config} {task}')
  182. if self.clients[config].gui_set_task(task, group, arg, value):
  183. return True
  184. else:
  185. return False
  186. else:
  187. logger.info(f'script {config} is not running')
  188. return False
  189. @Slot(str, str, str, str, bool, result="bool")
  190. def gui_set_task_bool(self, config: str, task: str, group: str, arg: str, value: bool) -> bool:
  191. """
  192. 设置task的gui 是bool类型的
  193. :param config:
  194. :param task:
  195. :param group:
  196. :param arg:
  197. :param value:
  198. :return:
  199. """
  200. if config in self.clients:
  201. logger.info(f'gui set value of {config} {task}')
  202. if self.clients[config].gui_set_task(task, group, arg, value):
  203. return True
  204. else:
  205. return False
  206. else:
  207. logger.info(f'script {config} is not running')
  208. return False
  209. @Slot(str, str, str, str, float, result="bool")
  210. def gui_set_task_number(self, config: str, task: str, group: str, arg: str, value) -> bool:
  211. """
  212. 设置task的gui 是float类型的或者是int
  213. :param config:
  214. :param task:
  215. :param group:
  216. :param arg:
  217. :param value:
  218. :return:
  219. """
  220. if config in self.clients:
  221. logger.info(f'gui set value of {config} {task}')
  222. if self.clients[config].gui_set_task(task, group, arg, value):
  223. return True
  224. else:
  225. return False
  226. else:
  227. logger.info(f'script {config} is not running')
  228. return False
  229. @Slot(str, result="QImage")
  230. def gui_mirror_image(self, config: str) -> QImage:
  231. """
  232. :param config:
  233. :return:
  234. """
  235. if config in self.clients:
  236. logger.info(f'gui get mirror image of {config}')
  237. # 接收流对象
  238. stream = self.clients[config].gui_mirror_image()
  239. # 创建 BytesIO 对象来存储图像数据
  240. buffer = io.BytesIO()
  241. for data in stream:
  242. buffer.write(data)
  243. # 将 BytesIO 对象的内容作为字节流解码为 cv2 图像
  244. buffer.seek(0) # 将读取位置重置为起始位置
  245. image_data = np.frombuffer(buffer.getvalue(), dtype=np.uint8)
  246. image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
  247. height, width, _ = image.shape
  248. image_qt = QImage(image.data, width, height, QImage.Format_RGB888).rgbSwapped()
  249. return image_qt
  250. else:
  251. logger.info(f'script {config} is not running')
  252. return None