Python3+Django3+Celery4+Rabbirmq 使用Celery,django配置信息

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',  # 跨域设置
    # 'djcelery',  # celery 定时任务
    'django_celery_beat',
    # 'django_rundirect',     # 脚本启动
    'archives',  # 工程管理
    'file_client',  # 文件管理 by jj
    'one_diagram',
    'conf_diagram',
    'system',  # 系统管理  by cjh
    'ladder_price',  # 用水户、阶梯水价、扣费  by cjh
    'lot_api',  # 数据接收  by cjh
    'maintain_manage',  # 运维模块
    'wechat_payment',
    'recharge', 
}

LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False

# rabbitMQ
RABBITMQ_IP = '127.0.0.1'
RABBITMQ_PORT = '5672'
RABBITMQ_USER = 'guest'
RABBITMQ_PWD = 'guest'

# celery配置 start
# BROKER_URL = 'amqp://' + RABBITMQ_USER + ':' + RABBITMQ_PWD + '@' + RABBITMQ_IP + ':' + RABBITMQ_PORT + '//'
BROKER_URL = 'amqp://guest:[email protected]:5672//'
now_time = datetime.datetime.now().strftime("%Y%m%d")
# 使用和Django一样的时区
CELERY_TIMEZONE = TIME_ZONE
# 使用本地触发celery任务
CELERY_ENABLE_UTC = False
# 解决不同时区时间类型,冲突
DJANGO_CELERY_BEAT_TZ_AWARE = True
# 任务结果的时效时间
CELERY_TASK_RESULT_EXPIRES = 60 * 10
# log路径
CELERYD_LOG_FILE = BASE_DIR + "/logs/celery/celery_{}.log".format(now_time)
# beat log路径
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']

# 以上为基本配置,以下为周期性任务定义,以celerybeat_开头的
# CELERYBEAT_SCHEDULER = 'djcelery.schedulers.DatabaseScheduler'
# CELERY_RESULT_BACKEND = 'djcelery.backends.database:DatabaseBackend'
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 默认队列
CELERY_DEFAULT_ROUTING_KEY = 'default_key'  # celery 默认队列键

启动服务:
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服务

你可能感兴趣的:(python,celery,Django)