process_manager.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. from PySide6.QtCore import QObject, Slot, Signal
  8. from module.gui.process.script_process import ScriptProcess
  9. from module.gui.context.add import Add
  10. from module.logger import logger
  11. def is_port_in_use(ip, port) -> bool:
  12. """
  13. 检查端口是否被占用
  14. :param ip:
  15. :param port:
  16. :return:
  17. """
  18. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  19. try:
  20. s.connect((ip, port))
  21. s.shutdown(2)
  22. logger.info(f'port {port} is in use')
  23. return True
  24. except:
  25. logger.info(f'port {port} is not in use')
  26. return False
  27. class ProcessManager(QObject):
  28. """
  29. 进程管理
  30. """
  31. def __init__(self) -> None:
  32. """
  33. init
  34. """
  35. super().__init__()
  36. self.processes = {} # 持有所有的进程
  37. self.ports = {} # 持有所有的端口
  38. self.clients = {} # zerorpc连接的客户端
  39. @Slot()
  40. def create_all(self) -> None:
  41. """
  42. 创建所有的配置实例
  43. :return:
  44. """
  45. configs = Add().all_script_files()
  46. for config in configs:
  47. self.add(config)
  48. def add(self, config: str) -> None:
  49. """
  50. add
  51. :param config:
  52. :return:
  53. """
  54. if config not in self.processes:
  55. port = 40000 + random.randint(0, 200)
  56. while is_port_in_use('127.0.0.1', port):
  57. port = 40000 + random.randint(0, 200)
  58. self.ports[config] = port
  59. self.processes[config] = ScriptProcess(config, port)
  60. logger.info(f'create script {config} on port {port}')
  61. try:
  62. self.clients[config] = zerorpc.Client()
  63. self.clients[config].connect(f'tcp://127.0.0.1:{self.ports[config]}')
  64. except:
  65. logger.exception(f'connect to script {config} error')
  66. raise
  67. self.processes[config].start()
  68. logger.info(f'add script {config}')
  69. else:
  70. logger.info(f'script {config} is already running')
  71. def remove(self, config: str) -> None:
  72. """
  73. remove
  74. :param config:
  75. :return:
  76. """
  77. if config in self.processes:
  78. self.processes[config].stop()
  79. del self.processes[config]
  80. del self.ports[config]
  81. del self.clients[config]
  82. logger.info(f'remove script {config}')
  83. logger.info(f'port {config} is released')
  84. else:
  85. logger.info(f'script {config} is not running')
  86. def restart(self, config: str) -> None:
  87. """
  88. restart
  89. :param config:
  90. :return:
  91. """
  92. if config in self.processes:
  93. self.processes[config].restart()
  94. logger.info(f'restart script {config}')
  95. else:
  96. logger.info(f'script {config} is not running')
  97. def stop_all(self) -> None:
  98. """
  99. stop_all
  100. :return:
  101. """
  102. for config in self.processes:
  103. self.processes[config].stop()
  104. logger.info(f'stop all script')
  105. def restart_all(self) -> None:
  106. """
  107. restart_all
  108. :return:
  109. """
  110. for config in self.processes:
  111. self.processes[config].restart()
  112. logger.info(f'restart all script')
  113. def get_client(self, config: str) -> zerorpc.Client:
  114. """
  115. get_client
  116. :param config:
  117. :return:
  118. """
  119. if config in self.clients:
  120. return self.clients[config]
  121. else:
  122. logger.info(f'script {config} is not running')
  123. return None
  124. @Slot(str, result="QString")
  125. def gui_menu(self, config: str) -> str:
  126. """
  127. get_gui_menu
  128. :param config:
  129. :return:
  130. """
  131. if config in self.clients:
  132. return self.clients[config].gui_menu()
  133. else:
  134. logger.info(self.clients)
  135. logger.info(f'script {config} is not running')
  136. return None
  137. @Slot(str, str, result="QString")
  138. def gui_args(self, config: str, task: str) -> str:
  139. """
  140. 获取显示task的gui参数
  141. :param config:
  142. :param task:
  143. :return:
  144. """
  145. if config in self.clients:
  146. return self.clients[config].gui_args(task)
  147. else:
  148. logger.info(f'script {config} is not running')
  149. return None