Search code examples
pythondjangocelery

Can i use celery without django


I have API code which adds tasks to queue and then celery workers consuming those tasks.

Currently I have both code base same. But i want celery workers just to have simple plain Python tasks and no django code as workers will only be processing tasks and does not need django for that. Is it possible.

In order to start celery worker i need to use this line

celery -A django_project worker --queue high

What should i write instead of django_project there


Solution

  • Yes you can. Celery is a generic asynchronous task queue. In place of "django_project" you would point to your module. See http://docs.celeryproject.org/en/latest/getting-started/first-steps-with-celery.html#application for an example.

    Here is an example project layout using celery:

    project-dir/
        mymodule/
             __init__.py 
             celery.py
             tasks.py
        tests/
        setup.py
        etc, etc (e.g. tox.ini, requirements.txt, project management files)
    

    In mymodule/celery.py:

    # -*- coding : utf-8 -*-
    from __future__ import absolute_import
    
    from celery import Celery
    
    app = Celery('mymodule',
                 broker='amqp://',
                 backend='amqp://',
                 include=['mymodule.tasks'])
    
    if __name__ == '__main__':
        app.start()
    

    In mymodule/tasks.py:

    from __future__ import absolute_import
    
    from mymodule.celery import app
    
    @app.task
    def add(x, y):
        return x + y