简体   繁体   中英

raise ConnectionError(self._error_message(e)) kombu.exceptions.OperationalError: Error 111 connecting to localhost:6379. Connection refused

minimal django/celery/redis is running locally, but when deployed to heroku gives me the following error, when I run on python:

 raise ConnectionError(self._error_message(e))
 kombu.exceptions.OperationalError: Error 111 connecting to localhost:6379. Connection     
 refused.

This is my tasks.py file in my application directory:

   from celery import Celery
   import os

   app = Celery('tasks', broker='redis://localhost:6379/0')

   app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
            CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

   @app.task
   def add(x, y):
      return x + y

Requirements.txt:

  django
  gunicorn
  django-heroku
  celery
  redis
  celery-with-redis
  django-celery
  kombu

I have set worker dyno to 1. Funny things is i could have sworn it was working before, now it doesnt work for some reason.

Once, you have a minimal django-celery-redis project setup on local, here is how you deploy it on heroku:

  1. Add to your tasks.py:

     import os app.conf.update(BROKER_URL=os.environ['REDIS_URL'], CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])
  2. Make sure your requirements.txt is like this:

     django gunicorn django-heroku celery redis
  3. Add to your Procfile: "worker: celery worker --app=hello.tasks.app"

  4. Make sure it still runs on local

  5. enter into terminal: "export REDIS_URL=redis://"

  6. run "heroku local&"

  7. run python

     import hello.tasks hello.tasks.add.delay(1,2)

Should return something like:

    <AsyncResult: e1debb39-b61c-47bc-bda3-ee037d34a6c4>
  1. "heroku apps:create minimal-django-celery-redis"

  2. "heroku addons:create heroku-redis -a minimal-django-celery-redis"

  3. "git add."

  4. "git commit -m "Demo""

  5. "git push heroku master"

  6. "heroku open&"

  7. "heroku ps:scale worker=1"

  8. "heroku run python"

     import hello.tasks hello.tasks.add.delay(1, 2)
  9. You should see the task running in the application logs: "heroku logs -t -p worker"

This solved it for me, i forgot to import celery in project/ init .py like so

from .celery import app as celery_app

__all__ = ("celery_app",)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM