script_process.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. from multiprocessing import Process
  5. from module.logger import logger
  6. class ScriptProcess(Process):
  7. def __init__(self, config: str, port: int) -> None:
  8. """
  9. :param port: 端口 tcp 127.0.0.1:port
  10. :param config: 如oas1
  11. """
  12. super().__init__()
  13. self.config = config
  14. self.port = port
  15. self.name = config
  16. self.daemon = True # 设置为守护进程,主进程结束,子进程也结束
  17. @property
  18. def alive(self) -> bool:
  19. """
  20. alive
  21. :return:
  22. """
  23. return self.is_alive()
  24. def run(self) -> None:
  25. """
  26. run
  27. :return:
  28. """
  29. try:
  30. from script import Script
  31. script = Script(config_name=self.config)
  32. script.init_server(self.port)
  33. script.run_server()
  34. except:
  35. logger.exception(f'run script {self.config} error')
  36. raise
  37. def stop(self) -> None:
  38. """
  39. stop the process
  40. 是强制的,不会等待子进程结束
  41. :return:
  42. """
  43. self.terminate()
  44. self.join()
  45. logger.info(f'stop script {self.config}')
  46. def restart(self) -> None:
  47. """
  48. restart the process
  49. :return:
  50. """
  51. self.stop()
  52. self.start()
  53. logger.info(f'restart script {self.config}')