process_manager.py 15 KB

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