Files

39 lines
872 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""模块:Celery 应用定义
异步任务体系入口,配合 settings.py 的 CELERY_* 配置使用。
启动 worker:
celery -A gvsds worker -l info
启动 beat 调度:
celery -A gvsds beat -l info
无 Celery 时项目仍可正常运行(任务同步执行),便于本地开发。
"""
import os
from celery import Celery
# 设置默认 DJANGO_SETTINGS_MODULE(若未设置)
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gvsds.settings')
App = Celery('gvsds')
# 从 settings.py 读取 CELERY_* 配置
App.config_from_object('django.conf:settings', namespace='CELERY')
# 自动发现各 app 的 tasks.py
App.autodiscover_tasks()
@App.task(bind=True)
def DebugTask(self):
"""调试任务,验证 Celery 连通性。"""
print(f'Request: {self.request!r}')
@App.task
def Ping():
"""简单心跳任务。"""
return 'pong'