process_manager.py 14 KB

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