celery 支撑 django-celery-beat启动服务,需安装俩包: pip install celery pip install django-celery-beat
记得同步表 python manage.py makemigrations Python manage migrate
django setting.py 文件配置信息:
INSTALLED_APPS
= (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'rest_framework.authtoken',
'corsheaders',
'django_celery_beat',
'archives',
'file_client',
'one_diagram',
'conf_diagram',
'system',
'ladder_price',
'lot_api',
'maintain_manage',
'wechat_payment',
'recharge',
}
LANGUAGE_CODE
= 'en-us'
TIME_ZONE
= 'Asia/Shanghai'
USE_I18N
= True
USE_L10N
= True
USE_TZ
= False
RABBITMQ_IP
= '127.0.0.1'
RABBITMQ_PORT
= '5672'
RABBITMQ_USER
= 'guest'
RABBITMQ_PWD
= 'guest'
BROKER_URL
= 'amqp://guest:guest@127.0.0.1:5672//'
now_time
= datetime
.datetime
.now
().strftime
("%Y%m%d")
CELERY_TIMEZONE
= TIME_ZONE
CELERY_ENABLE_UTC
= False
DJANGO_CELERY_BEAT_TZ_AWARE
= True
CELERY_TASK_RESULT_EXPIRES
= 60 * 10
CELERYD_LOG_FILE
= BASE_DIR
+ "/logs/celery/celery_{}.log".format(now_time
)
CELERYBEAT_LOG_FILE
= BASE_DIR
+ "/logs/celery/beat_{}.log".format(now_time
)
ERROR_LOG
= BASE_DIR
+ "/logs/error_{}.log".format(now_time
)
CELERY_ACCEPT_CONTENT
= ['pickle', 'json']
CELERY_RESULT_BACKEND
= 'redis://:'+REDIS_PASSWORD
+'@127.0.0.1:6379/14'
CELERY_TASK_IGNORE_RESULT
= False
CELERY_QUEUES
= (
Queue
('default_queue', Exchange
('default_queue', type='direct'), routing_key
='default_key'),
)
CELERY_DEFAULT_QUEUE
= 'default_queue'
CELERY_DEFAULT_ROUTING_KEY
= 'default_key'
启动服务: celery -A human_drink_yq beat -l info --scheduler # 启动beat,注册任务 django_celery_beat.schedulers:DatabaseScheduler celery worker -A # 启动worker,执行任务 human_drink_yq -P gevent celery flower -A human_drink_yq # 启动flower,监听任务 python manage.py runserver # 启动django服务