Python 芹菜时间表不起作用,使用Django时出现关键错误
Env:django 1.6.5 python 2.7芹菜3.1.11 我运行此项目的目标服务器未安装DJ芹菜。所以,我不是想用芹菜。 我正在跟踪文档和。当我运行芹菜-A djproj-B-l调试时,我得到了一个KeyError。实际上,[任务]中没有目标任务。有人知道怎么解决吗?谢谢 错误 djproj/djproj/u_初始值 djproj/djproj/芹菜.pyPython 芹菜时间表不起作用,使用Django时出现关键错误,python,django,redis,celery,celery-task,Python,Django,Redis,Celery,Celery Task,Env:django 1.6.5 python 2.7芹菜3.1.11 我运行此项目的目标服务器未安装DJ芹菜。所以,我不是想用芹菜。 我正在跟踪文档和。当我运行芹菜-A djproj-B-l调试时,我得到了一个KeyError。实际上,[任务]中没有目标任务。有人知道怎么解决吗?谢谢 错误 djproj/djproj/u_初始值 djproj/djproj/芹菜.py from __future__ import absolute_import import os import dateti
from __future__ import absolute_import
import os
import datetime
from celery import Celery
from django.conf import settings
# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'djproj.settings')
app = Celery('djproj')
# Using a string here means the worker will not have to
# pickle the object when using Windows.
app.config_from_object('django.conf:settings')
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
@app.task(bind=True)
def debug_task(self):
print('Request: {0!r}'.format(self.request))
@app.task
def defaulttask1():
currt = datetime.datetime.now()
with open("files/defaulttask1.txt", "w") as fo:
print >> fo, currt.isoformat()+"default_task_1"
return currt
@app.task
def hello():
print "helloincelery"
djproj/djproj/settings.py
"""
Django settings for djproj project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
from __future__ import absolute_import
from celery.schedules import crontab
BROKER_URL = 'redis://localhost:6379/0'
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
#'defaulttask1': {
# 'task': 'djproj.celery.defaulttask1',
# 'schedule': timedelta(seconds=3)
#},
'hello': {
'task': 'djproj.celery.hello',
'schedule': timedelta(seconds=4)
},
'tuantask1': {
'task': 'apps.app1.tuan.tuantask1',
'schedule': timedelta(seconds=6)
},
#'tuantask2': {
# 'task': 'app1.tuan.tuantask2',
# 'schedule': crontab(minute=55, hour=17)
#}
}
TIME_ZONE = 'Asia/Shanghai'
DATETIME_FORMAT = 'Y-m-d H:i:s'
TIME_FORMAT = 'Y-m-d H:i:s'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+df53@zaea16*pa%)kyta=ciam#$1c1&tjx-5!59f+nlo)n#!4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.app1',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'djproj.urls'
WSGI_APPLICATION = 'djproj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'zh_CN'
#TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
djproj/apps/app1/tuan.py
from __future__ import absolute_import
#from djproj.celery import app
from celery import shared_task
import datetime
#@shared_task
#def add(x, y):
# return x + y
#@shared_task
#def mul(x, y):
# return x * y
#@shared_task
#def xsum(numbers):
# return sum(numbers)
@shared_task
def tuantask1():
currt = datetime.datetime.now()
with open("files/tuantask1.txt", "w") as fo:
print >> fo, currt.isoformat()+"tuantask_1"
return currt.isoformat()
@shared_task
def tuantask2():
print "stest===="
djproj/run.sh
celery -A djproj worker -B -l debug
我知道会发生什么。我需要将tuan.py修改为tasks.py,因为芹菜中的自动发现_任务只能在名为tasks.py的文件中的应用中找到任务。如果不修改文件名,只需使用绝对路径(例如“apps.app1.tuan”)在settings.py中手动导入任务即可。看 |
"""
Django settings for djproj project.
For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""
from __future__ import absolute_import
from celery.schedules import crontab
BROKER_URL = 'redis://localhost:6379/0'
from datetime import timedelta
CELERYBEAT_SCHEDULE = {
#'defaulttask1': {
# 'task': 'djproj.celery.defaulttask1',
# 'schedule': timedelta(seconds=3)
#},
'hello': {
'task': 'djproj.celery.hello',
'schedule': timedelta(seconds=4)
},
'tuantask1': {
'task': 'apps.app1.tuan.tuantask1',
'schedule': timedelta(seconds=6)
},
#'tuantask2': {
# 'task': 'app1.tuan.tuantask2',
# 'schedule': crontab(minute=55, hour=17)
#}
}
TIME_ZONE = 'Asia/Shanghai'
DATETIME_FORMAT = 'Y-m-d H:i:s'
TIME_FORMAT = 'Y-m-d H:i:s'
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+df53@zaea16*pa%)kyta=ciam#$1c1&tjx-5!59f+nlo)n#!4'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.app1',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
ROOT_URLCONF = 'djproj.urls'
WSGI_APPLICATION = 'djproj.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/
LANGUAGE_CODE = 'zh_CN'
#TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/
STATIC_URL = '/static/'
from __future__ import absolute_import
#from djproj.celery import app
from celery import shared_task
import datetime
#@shared_task
#def add(x, y):
# return x + y
#@shared_task
#def mul(x, y):
# return x * y
#@shared_task
#def xsum(numbers):
# return sum(numbers)
@shared_task
def tuantask1():
currt = datetime.datetime.now()
with open("files/tuantask1.txt", "w") as fo:
print >> fo, currt.isoformat()+"tuantask_1"
return currt.isoformat()
@shared_task
def tuantask2():
print "stest===="
celery -A djproj worker -B -l debug
a common practice for reusable apps is to define all tasks in a separate
tasks.py module, and Celery does have a way to autodiscover these modules
This way you do not have to manually add the individual modules to the
CELERY_IMPORTS setting.