skip to Main Content

I have recently started with django. And I started doing a small project. I’ve been using celery with redis worker. And every to use celery and redis I have to run the celery and redis server and then django server. Which is a bit lengthy process.
I have two questions.
1. Am I doing the right thing by running the servers everytime or are there any other right method to this process?
2. If I’m in the right direction, is there any method to do this?

I tried circus.ini , but it did not work.

2

Answers


  1. If you use UNIX system:

    1. For this purpose you can get along just with bash. Just run celery and redis in background – use & command.
    redis-server & celery -A app_name worker -l info & python manage.py runserver
    

    Disadvantage of this approach – redis and celery will work in the background even after a shutdown of django dev server. So you need to terminate these processes. See this unix se answer for examples how to do that.

    So you can create 2 bash scripts start.sh (contains commands with &) and cleanup.sh (terminate processes) and run them respectively.

    For production see purpose #2

    1. Use systemd or supervisor. You need to create conf files for your daemons and then run them.
    Login or Signup to reply.
  2. Building upon Yevhenii M.‘s answer, you can start a subshell command with a trap to kill all running processes in that subshell when you hit Ctrl+C:

    (trap "kill 0" SIGINT; redis-server & celery -A app_name worker -l info & python manage.py runserver)
    

    or as a more readable multiline command:

    (
        trap "kill 0" SIGINT
        redis-server & 
        celery -A app_name worker -l info &
        python manage.py runserver
    )
    

    Another option is to use a Procfile manager, but that requires installing additional dependencies/programs. Something like foreman or one of it’s ports in other languages:

    (Source: foreman’s README)

    For this you create a Procfile (file in your project root) where you specify which commands to run:

    redis:  redis-server
    worker: celery -A app_name worker
    web:    python manage.py runserver
    

    Then run foreman start

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search