我正在尝试使用Heroku进行异步任务 . 我相信Celery不会正确地与Redis后端通信 . 如果你能帮助我排除故障,那就太棒了 . 谢谢你一千次 . 我按照这些教程:

Django with Celery:http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html Celery with Heroku:https://devcenter.heroku.com/articles/celery-heroku

当我运行 add.delay() 函数(在我的shared_tasks应用程序中添加一个函数)时,它似乎没有连接到Redis数据库 . 我输入延迟功能直到按下控制器C后它才会冻结 . 它看起来像:

C:\Users\Jup\Drop>heroku ps:scale worker=1
Scaling dynos... done, now running worker at 1:Hobby

C:\Users\Jup\Drop>heroku run python
Running python on shrouded-ocean-19461... up, run.6193 (Hobby)
Python .6.3 (default, Nov 14 2017, 17:29:48)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from Buylist.tasks import *
>>> add(2,4)
6
>>> mul(3,4,3)
36
>>> add.delay(3,4)

我应该看到通过 heroku logs -t -p 运行的任务,但当然我没有多次尝试阅读本教程,但我无法弄清楚问题是什么 . 日志看起来像:

C:\Users\Jup\Drop>heroku logs -t -p worker
2018-01-08T16:13:10.157170+00:00 heroku[worker.1]: Stopping all processes with SIGTERM
2018-01-08T16:13:10.199379+00:00 app[worker.1]:
2018-01-08T16:13:10.199562+00:00 app[worker.1]: worker: Warm shutdown (MainProcess)
2018-01-08T16:13:12.097078+00:00 heroku[worker.1]: Process exited with status 0
2018-01-08T16:13:49.229159+00:00 heroku[worker.1]: Starting process with command `celery worker --app=project.celery.app`
2018-01-08T16:13:53.620356+00:00 app[worker.1]:  -------------- celery@5c8f2bed-9da8-4773-9aab-34329d273171 v4.1.0 (latentcall)
2018-01-08T16:13:53.620359+00:00 app[worker.1]: --- * ***  * -- Linux-4.14.11-041411-generic-x86_64-with-debian-stretch-sid 2018-01-08 16:13:53
2018-01-08T16:13:53.620357+00:00 app[worker.1]: ---- **** -----
2018-01-08T16:13:53.620363+00:00 app[worker.1]: - ** ---------- .> transport:   redis://h:**@ec2-34-237-158-248.compute-1.amazonaws.com:34239//
2018-01-08T16:13:53.620360+00:00 app[worker.1]: - ** ---------- [config]
2018-01-08T16:13:53.620364+00:00 app[worker.1]: - ** ---------- .> results:     redis://h:**@ec2-34-237-158-248.compute-1.amazonaws.com:34239/
2018-01-08T16:13:53.620359+00:00 app[worker.1]: -- * - **** ---
2018-01-08T16:13:53.620366+00:00 app[worker.1]: -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
2018-01-08T16:13:53.620365+00:00 app[worker.1]: - *** --- * --- .> concurrency: 8 (prefork)
2018-01-08T16:13:53.620367+00:00 app[worker.1]: --- ***** -----
2018-01-08T16:13:53.620375+00:00 app[worker.1]:
2018-01-08T16:13:53.620370+00:00 app[worker.1]:
2018-01-08T16:13:53.620323+00:00 app[worker.1]:
2018-01-08T16:13:53.620368+00:00 app[worker.1]:  -------------- [queues]
2018-01-08T16:13:53.620361+00:00 app[worker.1]: - ** ---------- .> app:         project:0x7f85ef858cc0
2018-01-08T16:13:53.620369+00:00 app[worker.1]:                 .> celery           exchange=celery(direct) key=celery
2018-01-08T16:13:55.197654+00:00 app[worker.1]:   warnings.warn('Using settings.DEBUG leads to a memory leak, never '
2018-01-08T16:13:55.197599+00:00 app[worker.1]: [2018-01-08 16:13:55,197: WARNING/MainProcess] /app/.heroku/python/lib/python3.6/site-packages/celery/fixups/djang
o.py:202: UserWarning: Using settings.DEBUG leads to a memory leak, never use this setting in production environments!
2018-01-08T16:13:50.019080+00:00 heroku[worker.1]: State changed from starting to up

我只在celery.py中配置了Redis

project.celery

from __future__ import absolute_import, unicode_literals
import os
from celery import Celery
import redis


# set the default Django settings module for the 'celery' program.
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'project.settings')

app = Celery('project')

# Using a string here means the worker doesn't have to serialize
# the configuration object to child processes.
# - namespace='CELERY' means all celery-related configuration keys
#   should have a `CELERY_` prefix.
app.config_from_object('django.conf:settings', namespace='CELERY')
app.conf.update(CELERY_BROKER_URL=os.environ['REDIS_URL'],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

# Load task modules from all registered Django app configs.
app.autodiscover_tasks()


@app.task(bind=True)
def debug_task(self):
    print('Request: {0!r}'.format(self.request))

这是我的设置

项目/设置

from __future__ import absolute_import, unicode_literals
import dj_database_url


"""
Django settings for project project.

Generated by 'django-admin startproject' using Django 2.0.

For more information on this file, see
https://docs.djangoproject.com/en/2.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.0/ref/settings/
"""

import os
import django_heroku
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))



# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/2.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#Read secret key from a file

SECRET_KEY = 'KEY'



# SECURITY WARNING: don't run with debug turned on in production!
#DEBUG = True
DEBUG = bool( os.environ.get('DJANGO_DEBUG', True) )

ALLOWED_HOSTS = [
                'shrouded-ocean-19461.herokuapp.com', 'localhost', '127.0.0.1',

]

# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_celery_results',
    'django_celery_beat',
    'Buylist',
    #'django_q',

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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 = 'project.urls'

PROJECT_DIR = os.path.dirname(__file__)
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            os.path.join(PROJECT_DIR, "templates"),
        ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}


# Password validation
# https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/2.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/2.0/howto/static-files/

# Heroku: Update database configuration from $DATABASE_URL.

db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.10/howto/static-files/

# The absolute path to the directory where collectstatic will collect static files for deployment.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

# The URL tos use when referring to static files (where they will be served from)
STATIC_URL = '/static/'

# Simplified static file serving.
# https://warehouse.python.org/project/whitenoise/
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

#CELERY_RESULT_BACKEND = 'django-db'

#CELERY_BROKER_URL = 'amqp://localhost//'

我安装了Heroku-redis附加组件 . 如果重要的话,这是我的Procfile

Procfile

web: gunicorn project.wsgi --log-file -
worker: celery worker --app=project.celery.app