Friday, 15 April 2011

python - celery schedule not work and got KeyError using Django -



python - celery schedule not work and got KeyError using Django -

env: django 1.6.5 python 2.7 celery 3.1.11

my target server run project not install djcelery. so, not trying utilize djcelery. follow docsfirst steps django , using celery in application. when run celery -a djproj -b -l debug got keyerror. , there no target task in [tasks]. knows how solve it? thanks.

error

[tasks] . celery.backend_cleanup . celery.chain . celery.chord . celery.chord_unlock . celery.chunks . celery.group . celery.map . celery.starmap . djproj.celery.debug_task . djproj.celery.defaulttask1 . djproj.celery.hello ... ... [2014-06-19 16:08:23,007: info/mainprocess] received task: djproj.celery.hello[794e54c7-62c8-4ad8-bcbb-64ff809366d1] [2014-06-19 16:08:23,008: debug/mainprocess] taskpool: apply <function _fast_trace_task @ 0x1036d17d0> (args:('djproj.celery.hello', '794e54c7-62c8-4ad8-bcbb-64ff809366d1', [], {}, {'utc': true, u'is_eager': false, 'chord': none, u'group': none, 'args': [], 'retries': 0, u'delivery_info': {u'priority': 0, u'redelivered': none, u'routing_key': u'celery', u'exchange': u'celery'}, 'expires': none, u'hostname': 'celery@nluckys-mac.local', 'task': 'djproj.celery.hello', 'callbacks': none, u'correlation_id': u'794e54c7-62c8-4ad8-bcbb-64ff809366d1', 'errbacks': none, 'timelimit': (none, none), 'taskset': none, 'kwargs': {}, 'eta': none, u'reply_to': u'20fe5513-efc2-3e69-9541-8e82758f94f9', 'id': '794e54c7-62c8-4ad8-bcbb-64ff809366d1', u'headers': {}}) kwargs:{}) [2014-06-19 16:08:23,010: warning/worker-2] helloincelery [2014-06-19 16:08:23,010: error/mainprocess] received unregistered task of type 'apps.app1.tuan.tuantask1'. message has been ignored , discarded. did remember import module containing task? or maybe using relative imports? please see http://bit.ly/glye1c more information. total contents of message body was: {'utc': true, 'chord': none, 'args': [], 'retries': 0, 'expires': none, 'task': 'apps.app1.tuan.tuantask1', 'callbacks': none, 'errbacks': none, 'timelimit': (none, none), 'taskset': none, 'kwargs': {}, 'eta': none, 'id': 'f2f92c1d-ac6d-4131-a029-123fbfc4ab48'} (220b) traceback (most recent phone call last): file "/library/python/2.7/site-packages/celery/worker/consumer.py", line 455, in on_task_received strategies[name](message, body, keyerror: 'apps.app1.tuan.tuantask1'

shedule seems not working. total codes here. of codes pasted below.

my project folders:

djproj ├── apps │   ├── __init__.py │ └── app1 │      ├── __init__.py │      ├── admin.py │      ├── models.py │      ├── tests.py │      ├── tuan.py │      └── views.py ├── djproj │   ├── __init__.py │   ├── celery.py │   ├── settings.py │   ├── urls.py │   └── wsgi.py ├── files ├── manage.py └── run.sh

djproj/djproj/__init__.py

from __future__ import absolute_import # create sure app imported when # django starts shared_task utilize app. .celery import app celery_app

djproj/djproj/celery.py

from __future__ import absolute_import import os import datetime celery import celery django.conf import settings # set default django settings module 'celery' program. os.environ.setdefault('django_settings_module', 'djproj.settings') app = celery('djproj') # using string here means worker not have # pickle 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() open("files/defaulttask1.txt", "w") fo: print >> fo, currt.isoformat()+"default_task_1" homecoming currt @app.task def hello(): print "helloincelery"

djproj/djproj/settings.py

""" django settings djproj project. more info on file, see https://docs.djangoproject.com/en/1.6/topics/settings/ total list of settings , values, see https://docs.djangoproject.com/en/1.6/ref/settings/ """ __future__ import absolute_import celery.schedules import crontab broker_url = 'redis://localhost:6379/0' 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 within project this: os.path.join(base_dir, ...) import os base_dir = os.path.dirname(os.path.dirname(__file__)) # quick-start development settings - unsuitable production # see https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/ # security warning: maintain 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 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 celery import shared_task import datetime #@shared_task #def add(x, y): # homecoming x + y #@shared_task #def mul(x, y): # homecoming x * y #@shared_task #def xsum(numbers): # homecoming sum(numbers) @shared_task def tuantask1(): currt = datetime.datetime.now() open("files/tuantask1.txt", "w") fo: print >> fo, currt.isoformat()+"tuantask_1" homecoming currt.isoformat() @shared_task def tuantask2(): print "stest===="

djproj/run.sh

celery -a djproj worker -b -l debug

well, know happen. need modify tuan.py tasks.py, since autodiscover_tasks in celery.py can find tasks in apps in file named tasks.py. if not modify file name, import tasks manually using celery_imports in settings.py using absolute path (e.g. 'apps.app1.tuan' ) works either. see doc

a mutual practice reusable apps define tasks in separate tasks.py module, , celery have way autodiscover these modules

|

this way not have manually add together individual modules celery_imports setting.

python django redis celery celery-task

No comments:

Post a Comment