Clarification of guide to Heroku Celery

家住魔仙堡 提交于 2019-12-11 15:26:49

问题


I'm trying to figure out the woeful instructions here

Under the section "Configuring a Celery app" I'm not sure where i put the code:

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

Any clarification of these instructions is greatly appreciated.


回答1:


The instructions are indicating you should put that code in your tasks.py module. However, that's not exactly extensible for multiple packages, each with their own tasks.py module. What I'd recommend is creating a celery.py file in the same directory as your settings.py file.

# tasks.py
import celery
app = celery.Celery('example')
app.conf.update(BROKER_URL=os.environ['REDIS_URL'],
                CELERY_RESULT_BACKEND=os.environ['REDIS_URL'])

Or you can specify your settings in settings.py and configure celery as such:

# settings.py
broker_url = os.environ['REDIS_URL']
result_backend = os.environ['REDIS_URL']

# celery.py
from celery import Celery
from celery.utils.collections import DictAttribute
from celery.loaders.base import BaseLoader
from django.conf import settings
from django.apps import apps

class ProjectLoader(BaseLoader):
    def read_configuration(self):
        """Load configuration from Django settings.

        This may not be needed to be honest. It's what I use in my project.
        """
        return DictAttribute(settings)

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
# CELERY_LOADER must be set in the environment. Setting the ``loader``
# kwarg for the app instance does _not_ do what we need it to.
os.environ.setdefault("CELERY_LOADER", "project.celery:ProjectLoader")

app = Celery("project")
app.config_from_object("django.conf:settings")
app.autodiscover_tasks(lambda: [n.name for n in apps.get_app_configs()])

# Procfile
worker: celery worker --app=project.celery

Disclaimer, some of these configs will require adjustment for your project.




回答2:


Following is the steps i took to make a minimal heroku/django/celery/redis project in conjunction with the instructions here along with other sources I found on the web. Hopefully someone will find this useful.

  1. In your terminal, use the "heroku login" command to log in to the Heroku CLI.

  2. "git clone https://github.com/heroku/python-getting-started.git" to copy a basic django skeleton project to your local.

  3. rename python-getting-started to whatever.

  4. cd into this directory.

  5. run the following command: "pip install -r requirements.txt" Note: Postgres must be properly installed in order for this step to work properly.

  6. run the following command: "python manage.py collectstatic"

  7. Install redis on Mac: "brew install redis"

  8. Start redis server: "redis-server&" (The & at the end is to run it as a background process)

  9. Test if Redis server is running: "redis-cli ping". If it replies “PONG”, then it’s good to go!

  10. Install celery: "pip install celery"

  11. Make a tasks.py file in your application directory with the following code:

     from celery import Celery
    
     app = Celery('tasks', broker='redis://localhost:6379/0')
    
     @app.task
     def add(x, y):
         return x + y
    
  12. "cd .." back into root directory.

  13. Run celery: "celery worker -A=location of tasks&"

  14. run: "python manage.py shell" in your root directory.

  15. As your tasks celery server has been started, you can now use it to run your task just by importing tasks.py script, e.g from Python interpreter interactive mode:

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

This should return an Async message.

  1. Push your local to heroku master.

** Note: If you run "celery worker -A=location of tasks.py&" and it gives the message:

consumer: Cannot connect to amqp://guest:**@127.0.0.1:5672//: [Errno 61]           
Connection refused.

Try restarting the redis server with the command: "brew services restart redis"


There you have it. A minimal heroku/django/celery/redis project! You can download it here.Instructions on how to deploy this to heroku.

** Note: In the working project the "celery worker" command is already included in the Procfile.



来源:https://stackoverflow.com/questions/58884219/clarification-of-guide-to-heroku-celery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!