App.py 855 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # @description:
  2. # @author: licanglong
  3. # @date: 2025/10/11 11:10
  4. import logging
  5. import threading
  6. from abc import ABC, abstractmethod
  7. from typing import Optional
  8. from app.core import EM
  9. from app.handler import ApplicationStartupEvent
  10. _log = logging.getLogger(__name__)
  11. class App(ABC):
  12. """
  13. app 模块
  14. """
  15. _instance_lock = threading.Lock()
  16. _instance: Optional["App"] = None
  17. def __new__(cls):
  18. if cls._instance is None:
  19. with cls._instance_lock:
  20. if cls._instance is None:
  21. cls._instance = super().__new__(cls)
  22. return cls._instance
  23. def startup(self):
  24. EM.emit(ApplicationStartupEvent())
  25. @abstractmethod
  26. def run(self, *args, **kwargs):
  27. pass
  28. def start(self, *args, **kwargs):
  29. self.startup()
  30. self.run(*args, **kwargs)