script_process.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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) -> None:
  8. """
  9. :param config: 如oas1
  10. """
  11. super().__init__()
  12. self.config = config
  13. self.name = config
  14. self.daemon = True # 设置为守护进程,主进程结束,子进程也结束
  15. @property
  16. def alive(self) -> bool:
  17. """
  18. alive
  19. :return:
  20. """
  21. return self.is_alive()
  22. def run(self) -> None:
  23. """
  24. run
  25. :return:
  26. """
  27. try:
  28. from script import Script
  29. script = Script(config_name=self.config)
  30. except:
  31. logger.exception(f'run script {self.config} error')
  32. raise
  33. def stop(self) -> None:
  34. """
  35. stop the process
  36. 是强制的,不会等待子进程结束
  37. :return:
  38. """
  39. self.terminate()
  40. self.join()
  41. logger.info(f'stop script {self.config}')
  42. def restart(self) -> None:
  43. """
  44. restart the process
  45. :return:
  46. """
  47. self.stop()
  48. self.start()
  49. logger.info(f'restart script {self.config}')