process_manager.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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, Dict
  12. from cached_property import cached_property
  13. from PySide6.QtCore import QObject, Slot, Signal, Property
  14. from PySide6.QtGui import QImage
  15. from queue import Queue, Empty
  16. from rich.console import Console
  17. from multiprocessing.managers import SyncManager
  18. # from multiprocessing import Queue
  19. from threading import Thread
  20. from module.gui.process.script_process import ScriptProcess
  21. from module.config.config_menu import ConfigMenu
  22. from module.gui.context.add import Add
  23. from module.logger import logger
  24. def is_port_in_use(ip, port) -> bool:
  25. """
  26. 检查端口是否被占用
  27. :param ip:
  28. :param port:
  29. :return:
  30. """
  31. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  32. try:
  33. s.connect((ip, port))
  34. s.shutdown(2)
  35. logger.info(f'port {port} is in use')
  36. return True
  37. except:
  38. logger.info(f'port {port} is not in use')
  39. return False
  40. class ProcessManager(QObject):
  41. """
  42. 进程管理
  43. """
  44. log_signal = Signal(str, str) # 日志信号
  45. def __init__(self) -> None:
  46. """
  47. init
  48. """
  49. super().__init__()
  50. self.processes: Dict[str, ScriptProcess] = {} # 持有所有的进程
  51. self.ports: Dict[str, int] = {} # 持有所有的端口
  52. self.clients = {} # zerorpc连接的客户端
  53. self.manager = SyncManager() # 管理器
  54. self.manager.start()
  55. self.log_queue: Dict[str, Queue] = {} # 日志队列
  56. self.log_thread: Dict[str, Thread] = {} # 日志线程
  57. self.update_queue: Queue = None # 每次更新任务的时候push进来给gui显示
  58. self.update_thread: Thread = None # 更新线程
  59. self.start_update_tasks() # 启动更新线程
  60. @Slot()
  61. def create_all(self) -> None:
  62. """
  63. 创建所有的配置实例
  64. :return:
  65. """
  66. configs = Add().all_script_files()
  67. for config in configs:
  68. self.add(config)
  69. @Slot(str)
  70. def add(self, config: str) -> None:
  71. """
  72. add
  73. :param config: 如oas1
  74. :return:
  75. """
  76. if config not in self.processes:
  77. # 初始化端口
  78. port = 40000 + random.randint(0, 200)
  79. while is_port_in_use('127.0.0.1', port):
  80. port = 40000 + random.randint(0, 200)
  81. self.ports[config] = port
  82. # 初始化日志队列
  83. q = self.start_log(config)
  84. # 初始化启动进程
  85. self.processes[config] = ScriptProcess(config, port, q, update_queue=self.update_queue)
  86. self.log_thread[config].start()
  87. # 下面是启动 zerorpc 客户端
  88. logger.info(f'create script {config} on port {port}')
  89. try:
  90. self.clients[config] = zerorpc.Client()
  91. self.clients[config].connect(f'tcp://127.0.0.1:{self.ports[config]}')
  92. except:
  93. logger.exception(f'connect to script {config} error')
  94. raise
  95. self.processes[config].start()
  96. logger.info(f'add script {config}')
  97. else:
  98. logger.info(f'script {config} is already running')
  99. def remove(self, config: str) -> None:
  100. """
  101. remove
  102. :param config:
  103. :return:
  104. """
  105. if config in self.processes:
  106. self.processes[config].stop()
  107. del self.processes[config]
  108. del self.ports[config]
  109. del self.clients[config]
  110. logger.info(f'remove script {config}')
  111. logger.info(f'port {config} is released')
  112. else:
  113. logger.info(f'script {config} is not running')
  114. @Slot(str)
  115. def restart(self, config: str) -> None:
  116. """
  117. restart 重启某个进程(会重启zerorpc),但是ports、clients、log_queue、log_thread不会重启
  118. :param config:
  119. :return:
  120. """
  121. if config in self.processes:
  122. if not self.processes[config].is_alive():
  123. self.processes[config].start()
  124. logger.info(f'restart script {config}')
  125. return None
  126. self.processes[config].terminate() # 强制结束进程
  127. if self.ports[config] is None:
  128. logger.error(f'{config} port {config} is None')
  129. if self.clients[config] is None:
  130. logger.error(f'{config} client {config} is None')
  131. if self.log_queue[config] is None:
  132. logger.info(f'{config} log_queue {config} is None')
  133. self.log_queue[config] = self.manager.Queue()
  134. if self.log_thread[config] is None or self.log_thread[config].is_alive() is False:
  135. logger.info(f'{config} log_thread {config} is None')
  136. self.processes[config] = ScriptProcess(config=config,
  137. port=self.ports[config],
  138. log_queue=self.log_queue[config],
  139. update_queue=self.update_queue)
  140. self.processes[config].start()
  141. logger.info(f'restart script {config}')
  142. else:
  143. logger.info(f'script {config} is not running')
  144. def stop_all(self) -> None:
  145. """
  146. stop_all
  147. :return:
  148. """
  149. for config in self.processes:
  150. self.processes[config].stop()
  151. logger.info(f'stop all script')
  152. def restart_all(self) -> None:
  153. """
  154. restart_all
  155. :return:
  156. """
  157. for config in self.processes:
  158. self.processes[config].restart()
  159. logger.info(f'restart all script')
  160. def get_client(self, config: str) -> zerorpc.Client:
  161. """
  162. get_client
  163. :param config:
  164. :return:
  165. """
  166. if config in self.clients:
  167. return self.clients[config]
  168. else:
  169. logger.info(f'script {config} is not running')
  170. return None
  171. @Slot(result="QString")
  172. def gui_menu(self) -> str:
  173. """
  174. get_gui_menu
  175. :param config:
  176. :return:
  177. """
  178. menu = ConfigMenu()
  179. return menu.gui_menu
  180. @Slot(str, str, result="QString")
  181. def gui_args(self, config: str, task: str) -> str:
  182. """
  183. 获取显示task的gui参数
  184. :param config:
  185. :param task:
  186. :return:
  187. """
  188. if config in self.clients:
  189. logger.info(f'gui get args of {config} {task}')
  190. return self.clients[config].gui_args(task)
  191. else:
  192. logger.info(f'script {config} is not running')
  193. return None
  194. @Slot(str, str, result="QString")
  195. def gui_task(self, config: str, task: str) -> str:
  196. """
  197. 获取显示task的gui
  198. :param config:
  199. :param task:
  200. :return:
  201. """
  202. if config in self.clients:
  203. logger.info(f'gui get value of {config} {task}')
  204. return self.clients[config].gui_task(task)
  205. else:
  206. logger.info(f'script {config} is not running')
  207. return None
  208. @Slot(str, str, str, str, str, result="bool")
  209. def gui_set_task(self, config: str, task: str, group: str, arg: str, value) -> bool:
  210. """
  211. 设置task的gui 是string类型的
  212. :param config:
  213. :param task:
  214. :param group:
  215. :param arg:
  216. :param value:
  217. :return:
  218. """
  219. if config in self.clients:
  220. logger.info(f'gui set value of {config} {task}')
  221. if self.clients[config].gui_set_task(task, group, arg, value):
  222. return True
  223. else:
  224. return False
  225. else:
  226. logger.info(f'script {config} is not running')
  227. return False
  228. @Slot(str, str, str, str, bool, result="bool")
  229. def gui_set_task_bool(self, config: str, task: str, group: str, arg: str, value: bool) -> bool:
  230. """
  231. 设置task的gui 是bool类型的
  232. :param config:
  233. :param task:
  234. :param group:
  235. :param arg:
  236. :param value:
  237. :return:
  238. """
  239. if config in self.clients:
  240. logger.info(f'gui set value of {config} {task}')
  241. if self.clients[config].gui_set_task(task, group, arg, value):
  242. return True
  243. else:
  244. return False
  245. else:
  246. logger.info(f'script {config} is not running')
  247. return False
  248. @Slot(str, str, str, str, float, result="bool")
  249. def gui_set_task_number(self, config: str, task: str, group: str, arg: str, value) -> bool:
  250. """
  251. 设置task的gui 是float类型的或者是int
  252. :param config:
  253. :param task:
  254. :param group:
  255. :param arg:
  256. :param value:
  257. :return:
  258. """
  259. if config in self.clients:
  260. logger.info(f'gui set value of {config} {task}')
  261. if self.clients[config].gui_set_task(task, group, arg, value):
  262. return True
  263. else:
  264. return False
  265. else:
  266. logger.info(f'script {config} is not running')
  267. return False
  268. @Slot(str, result="QImage")
  269. def gui_mirror_image(self, config: str) -> QImage:
  270. """
  271. :param config:
  272. :return:
  273. """
  274. if config in self.clients:
  275. logger.info(f'gui get mirror image of {config}')
  276. # 接收流对象
  277. stream = self.clients[config].gui_mirror_image()
  278. # 创建 BytesIO 对象来存储图像数据
  279. buffer = io.BytesIO()
  280. for data in stream:
  281. buffer.write(data)
  282. # 将 BytesIO 对象的内容作为字节流解码为 cv2 图像
  283. buffer.seek(0) # 将读取位置重置为起始位置
  284. image_data = np.frombuffer(buffer.getvalue(), dtype=np.uint8)
  285. image = cv2.imdecode(image_data, cv2.IMREAD_COLOR)
  286. height, width, _ = image.shape
  287. image_qt = QImage(image.data, width, height, QImage.Format_RGB888).rgbSwapped()
  288. return image_qt
  289. else:
  290. logger.info(f'script {config} is not running')
  291. return None
  292. def start_log(self, config_name: str) -> Queue:
  293. """
  294. 启动某个脚本实例config_name的log: 具体为创建一个queue信息队列,然后创建一个log线程,将queue传入log线程
  295. :param config_name:
  296. :return:
  297. """
  298. # if config_name not in self.processes:
  299. # logger.error(f'Process manager has no config {config_name}')
  300. # logger.info(f'Script {config_name} is not running')
  301. # return None
  302. if config_name not in self.log_queue:
  303. self.log_queue[config_name] = self.manager.Queue()
  304. if config_name not in self.log_thread:
  305. self.log_thread[config_name] = Thread(target=self.log_thread_func, args=(config_name,), daemon=True)
  306. return self.log_queue[config_name]
  307. def log_thread_func(self, config_name: str) -> None:
  308. """
  309. log线程函数,将queue中的信息解析
  310. :param config_name:
  311. :return:
  312. """
  313. q = self.log_queue[config_name] if config_name in self.log_queue else None
  314. if q is None:
  315. logger.error(f'Process manager has no config {config_name}')
  316. logger.info(f'Script {config_name} is not running')
  317. return
  318. console = Console()
  319. # while self.processes[config_name].is_alive():
  320. while True:
  321. try:
  322. log = q.get(timeout=1)
  323. if log is None:
  324. continue
  325. self.log_signal.emit(config_name, str(log))
  326. except Empty:
  327. continue
  328. except Exception as e:
  329. logger.error(f'Log thread of {config_name} error: {e}')
  330. break
  331. def start_update_tasks(self) -> None:
  332. """
  333. 启动更新任务的线程
  334. :return:
  335. """
  336. if self.update_queue is not None and self.update_thread is not None:
  337. logger.error(f'Update thread has already started')
  338. self.update_queue = self.manager.Queue()
  339. self.update_thread = Thread(target=self.update_thread_func, daemon=True)
  340. self.update_thread.start()
  341. def update_thread_func(self) -> None:
  342. """
  343. 更新 任务的 线程函数
  344. 从self.update_queue中获取信息,然后解析, 推送到gui
  345. :return:
  346. """
  347. logger.info(f'Update thread start')
  348. while self.update_thread.is_alive():
  349. try:
  350. update = self.update_queue.get(timeout=1)
  351. if update is None:
  352. continue
  353. logger.info(f'Update thread get')
  354. except Empty:
  355. continue
  356. except Exception as e:
  357. logger.error(f'Update thread error: {e}')
  358. break