2012-07-22 93 views
1

我使用亚马逊网络服务。我正在努力让芹菜工作。 试图做文档所说的(http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html#configuring-your-django-project-to-use-celery),但是当我启动芹菜工人并尝试调用一项任务时,没有任何事情发生,就好像没有任何任务一样。安装了芹菜,但它没有执行任务

这是我的settings.py文件:

import os 
import djcelery 
import djkombu 
import sys 
import tasks 
sys.path.append(os.getcwd()) 

djcelery.setup_loader() 

# Django settings for analogg project. 

DEBUG = False 
TEMPLATE_DEBUG = DEBUG 

ADMINS = (
    ('Nikita', '[email protected]'), 
) 

MANAGERS = ADMINS 

DATABASES = { 
    'default': { 
     'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'. 
     'NAME': 'analoggdb',      # Or path to database file if using sqlite3. 
     'USER': '',      # Not used with sqlite3. 
     'PASSWORD': '',     # Not used with sqlite3. 
     'HOST': '',      # Set to empty string for localhost. Not used with sqlite3. 
     'PORT': '',      # Set to empty string for default. Not used with sqlite3. 
    } 
} 

# Local time zone for this installation. Choices can be found here: 
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name 
# although not all choices may be available on all operating systems. 
# On Unix systems, a value of None will cause Django to use the same 
# timezone as the operating system. 
# If running in a Windows environment this must be set to the same as your 
# system time zone. 
TIME_ZONE = 'America/Chicago' 

# Language code for this installation. All choices can be found here: 
# http://www.i18nguy.com/unicode/language-identifiers.html 
LANGUAGE_CODE = 'en-us' 

SITE_ID = 1 

# If you set this to False, Django will make some optimizations so as not 
# to load the internationalization machinery. 
USE_I18N = True 

# If you set this to False, Django will not format dates, numbers and 
# calendars according to the current locale. 
USE_L10N = True 

# If you set this to False, Django will not use timezone-aware datetimes. 
USE_TZ = True 

# Absolute filesystem path to the directory that will hold user-uploaded files. 
# Example: "/home/media/media.lawrence.com/media/" 
MEDIA_ROOT = '' 

# URL that handles the media served from MEDIA_ROOT. Make sure to use a 
# trailing slash. 
# Examples: "http://media.lawrence.com/media/", "http://example.com/media/" 
MEDIA_URL = '/static/' 

# Absolute path to the directory static files should be collected to. 
# Don't put anything in this directory yourself; store your static files 
# in apps' "static/" subdirectories and in STATICFILES_DIRS. 
# Example: "/home/media/media.lawrence.com/static/" 
SITE_ROOT = os.path.dirname(os.path.realpath(__file__)) 
STATIC_ROOT = os.path.join(SITE_ROOT, 'static') 

# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = 'http://analogg.info/static/' 

# Additional locations of static files 
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

# List of finder classes that know how to find static files in 
# various locations. 
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder', 
    'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
    'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

ADMIN_MEDIA_PREFIX = '/static/admin/' 

# List of callables that know how to import templates from various sources. 
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader', 
    'django.template.loaders.app_directories.Loader', 
#  'django.template.loaders.eggs.Loader', 
) 

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    # Uncomment the next line for simple clickjacking protection: 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 

ROOT_URLCONF = 'analogg.urls' 

# Python dotted path to the WSGI application used by Django's runserver. 
WSGI_APPLICATION = 'analogg.wsgi.application' 

TEMPLATE_DIRS = (
    "/templates/", 
    "/home/ubuntu/analogg/templates", 
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 

INSTALLED_APPS = (
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    'django.contrib.messages', 
    'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    'django.contrib.admindocs', 
) 

INSTALLED_APPS += ('djcelery',) 
#INSTALLED_APPS += ('djkombu',) 

CELERYBEAT_SCHEDULER = "djcelery.schedulers.DatabaseScheduler" 

CELERY_SEND_TASK_ERROR_EMAILS = True 
CELERY_DISABLE_RATE_LIMITS = True 
CELERY_IMPORTS = ("analogg.tasks",) 

#BROKER_URL = 'amqp://guest:[email protected]:5672/' 
#BROKER_BACKEND = "djkombu.transport.DatabaseTransport" 


# A sample logging configuration. The only tangible logging 
# performed by this configuration is to send an email to 
# the site admins on every HTTP 500 error when DEBUG=False. 
# See http://docs.djangoproject.com/en/dev/topics/logging for 
# more details on how to customize your logging configuration. 
LOGGING = { 
    'version': 1, 
    'disable_existing_loggers': False, 
    'filters': { 
     'require_debug_false': { 
      '()': 'django.utils.log.RequireDebugFalse' 
     } 
    }, 
    'handlers': { 
     'mail_admins': { 
      'level': 'ERROR', 
      'filters': ['require_debug_false'], 
      'class': 'django.utils.log.AdminEmailHandler' 
     } 
    }, 
    'loggers': { 
     'django.request': { 
      'handlers': ['mail_admins'], 
      'level': 'ERROR', 
      'propagate': True, 
     }, 
    } 
} 

这是我的views.py文件:

# -*- coding: utf-8 -*- 
from django.http import HttpResponseRedirect, HttpResponse 
from django.template import Context, loader, RequestContext 
from django.shortcuts import render_to_response 
from tasks import parsesob, add 
from parsersob import parser 
import os, random, string 

def index(request): 
    html = "<html><body>Hello, world!</body></html>" 
    add.apply_async(args=[id, '23bnn']) 
    return HttpResponse(html) 

这是我tasks.py文件:

from celery.decorators import task 
from celery.task.schedules import crontab 
from parsersob import parser 
#from parsersob2 import parser2 
from celery import Celery 

#celery = Celery('tasks', broker='amqp://[email protected]//') 

@task 
def add(id, a1, a2): 
    f = open('add.txt', 'w') 
    g = a1 + a2 
    f.write(g) 
    f.close() 

而且这是我的celeryconfig.py文件与tasks.py,settings.py和views.py位于同一个文件夹中:

#BROKER_URL = 'amqp://' 
#CELERY_RESULT_BACKEND = 'analoggdb' 
#BROKER_URL = "amqp://guest:[email protected]:5672//" 
CELERY_IMPORTS = ("analogg.tasks",) 
#CELERY_RESULT_DBURI = "sqlite:///analoggdb.db" 

CELERY_TASK_SERIALIZER = 'json' 
CELERY_RESULT_SERIALIZER = 'json' 
CELERY_TIMEZONE = 'Europe/Moscow' 
CELERY_ENABLE_UTC = True 

我是一个新手,所以任何人都可以告诉我该怎么做才能使它工作。 任何帮助将不胜感激。

UPD:

[email protected]:/var/log/apache2$ sudo rabbitmqctl status 
Status of node '[email protected]' ... 
[{pid,849}, 
{running_applications,[{rabbit,"RabbitMQ","2.7.1"}, 
         {os_mon,"CPO CXC 138 46","2.2.7"}, 
         {sasl,"SASL CXC 138 11","2.1.10"}, 
         {mnesia,"MNESIA CXC 138 12","4.5"}, 
         {stdlib,"ERTS CXC 138 10","1.17.5"}, 
         {kernel,"ERTS CXC 138 10","2.14.5"}]}, 
{os,{unix,linux}}, 
{erlang_version,"Erlang R14B04 (erts-5.8.5) [source] [64-bit] [rq:1] [async-threads:30] [kernel-poll:true]\n"}, 
{memory,[{total,25494568}, 
      {processes,11083752}, 
      {processes_used,11077008}, 
      {system,14410816}, 
      {atom,1124433}, 
      {atom_used,1120234}, 
      {binary,89696}, 
      {code,11134393}, 
      {ets,752120}]}, 
{vm_memory_high_watermark,0.3999999990304762}, 
{vm_memory_limit,247544217}] 
...done. 

运行celeryd的输出:

[email protected]:~/analogg$ python manage.py celeryd worker --loglevel=info 

-------------- [email protected] v3.0.3 (Chiastic Slide) 
---- **** ----- 
--- * *** * -- [Configuration] 
-- * - **** --- . broker:  amqp://[email protected]:5672// 
- ** ---------- . app:   default:0x13c2a90 (djcelery.loaders.DjangoLoader) 
- ** ---------- . concurrency: 1 (processes) 
- ** ---------- . events:  OFF (enable -E to monitor this worker) 
- ** ---------- 
- *** --- * --- [Queues] 
-- ******* ---- . celery:  exchange:celery(direct) binding:celery 
--- ***** ----- 

[Tasks] 
    . analogg.tasks.add 
    . analogg.tasks.parsesob 

[2012-07-22 06:24:30,336: WARNING/MainProcess] [email protected] has started. 

UPD:

[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] mod_wsgi (pid=7411): Exception occurred processing WSGI script '/home/ubuntu/analogg/apache/django.wsgi'. 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] Traceback (most recent call last): 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__ 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  response = self.get_response(request) 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 82, in get_response 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  urlconf = settings.ROOT_URLCONF 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 185, in inner 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  return func(self._wrapped, *args) 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] mod_wsgi (pid=7377): Exception occurred processing WSGI script '/home/ubuntu/analogg/apache/django.wsgi'. 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] Traceback (most recent call last): 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__ 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  response = self.get_response(request) 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 82, in get_response 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  urlconf = settings.ROOT_URLCONF 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 185, in inner 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126]  return func(self._wrapped, *args) 
[Sun Jul 22 08:10:58 2012] [error] [client 37.110.0.126] AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF' 
+0

由于您已经评论了行#BROKER_URL ='amqp://'',您使用哪个代理然后? – Tisho 2012-07-22 11:12:24

+0

我只是在试验celeryconfig.py。我知道,它不应该被评论。但即使我没有注释它仍然没有任何反应。 – Nikita 2012-07-22 11:19:27

+0

问题是你有一个队列后端安装和运行 - 就像RabbitMQ(这是一个艰难的工作安装在亚马逊实例)或类似的?为了使任务能够运行,需要在代理中排队。尝试在配置中设置'CELERY_ALWAYS_EAGER = True',看看它是否工作... – Tisho 2012-07-22 11:22:15

回答

0

我不太清楚是什么问题在这里,但这里是一个示例配置,工作:

这是位于settings.py结束:

#CELERY CONFIG 
import djcelery 
djcelery.setup_loader() 

BROKER_HOST = "Host" 
BROKER_PORT = "Port" 
BROKER_USER = "User" 
BROKER_PASSWORD = "Pass" 
BROKER_VHOST = "VHost" 

CELERY_DEFAULT_QUEUE = "some_queue" 
CELERY_DEFAULT_EXCHANGE = "some_exc" 

经纪人的用户名/密码/虚拟主机使用配置了rabbitmqctl tool

您可以跟踪与队列/交换

rabbitmqadmin get queue="some_queue" 

注意:跟踪/var/log/rabbitmq/[email protected]中的日志或任何日志文件是非常有用的。这将告诉你,如果与RabbitMQ的连接有问题,如错误凭据,权限等。

+0

你猜这个问题是在rabbitmq设置? – Nikita 2012-07-22 11:47:27

+0

正如我所看到的其他设置看起来不错。一旦调试这样的情况,我花了很多时间,最后这是一个权限问题。所以看看rabbitmq日志 - 如果问题出现,它会给你所需的信息。 – Tisho 2012-07-22 11:49:45

+0

这里有一个很好的解释:http://sakaijunsoccer.appspot.com/media/img/skills/program/RabbitMQ.txt – Tisho 2012-07-22 12:26:11