1、安装 django-celery、celery、django-redis等一系列package
2、setting.py设置:
djcelery.setup_loader() BROKER_URL = ‘redis://127.0.0.1:6379/0‘ CELERY_TIMEZONE=‘Asia/Shanghai‘ #并没有北京时区,与下面TIME_ZONE应该一致 CELERYBEAT_SCHEDULER = ‘djcelery.schedulers.DatabaseScheduler‘
celery.py设置:
from __future__ import absolute_import import os import django from django.apps import apps, AppConfig from celery import Celery from django.conf import settings os.environ.setdefault(‘DJANGO_SETTINGS_MODULE‘, ‘backend.settings‘) django.setup() app = Celery(‘backend‘) app.config_from_object(‘django.conf:settings‘) #app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()]) installed_apps = [app_config.name for app_config in apps.get_app_configs()] app.autodiscover_tasks(lambda: installed_apps, force=True) @app.task(bind=True) def debug_task(self): print(‘Request: {0!r}‘.format(self.request))
apps/app/task.py
from __future__ import absolute_import from django.core.mail import send_mail from celery import task, shared_task from backend.settings import DEFAULT_FROM_EMAIL # 导入redis模 @task def send_email(vehicleorder): dosomething
3、运行:
python manage.py runserver
再运行:
python manage.py celery worker --loglevel=info
4、注意点 :
1、virtualbox下ubuntu运行django-web项目,物理机访问虚拟机,需要在命令后面加上虚拟机IP和端口python manage.py runserver 192.168.2.249:8000,同时在setting.py内加上allowed_host 虚拟机IP.
2、makemigrations 需要删除 migrations/文件夹下的文件,删除对应的sql表,还要在database中的migrations表中删除记录,缺一不可
原文地址:https://www.cnblogs.com/kkkboshow/p/9409448.html
时间: 2024-11-02 15:42:14