process_manager.py 8.2 KB

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