manager.py 989 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # -*- coding: utf-8 -*-
  2. # @Author : YY
  3. from concurrent.futures import Future
  4. from typing import Callable
  5. from flask import current_app
  6. from ruoyi_framework.config import ThreadPoolConfig
  7. class TaskManager:
  8. Tasks = []
  9. @classmethod
  10. def execute(cls, runner:Callable, *args, **kwargs):
  11. """
  12. 执行任务
  13. Args:
  14. runner (Callable): 任务函数
  15. *args: 任务函数参数
  16. **kwargs: 任务函数关键字参数
  17. """
  18. app = current_app._get_current_object()
  19. task = ThreadPoolConfig.thread_pool().submit(runner,app, *args, **kwargs)
  20. task.add_done_callback(cls.on_complete)
  21. if task and task not in cls.Tasks:
  22. cls.Tasks.append(task)
  23. @classmethod
  24. def on_complete(cls, fu:Future):
  25. """
  26. 任务完成回调
  27. Args:
  28. fu (Future): 任务Future对象
  29. """
  30. if fu.exception():
  31. raise fu._exception