Search code examples
celerydjango-celerydjango-signals

django - difference between signals and celery


This may be a lame question, but I am really confused with these two. I know signals are used to do some task when something has happened. But what about celery? In the documentation it says:

Celery is an asynchronous task queue/job queue based on distributed message passing.

Will someone please explain to me of what celery is? What's the difference between these two and when to use them? Will be much appreciated! Thank you.


Solution

  • Fist of all django signals are synchronous. For example if you have a signal which handle before_save action of SomeModel. And you have ... say ... view function like this:

    def some_view(requrest):
        # do some staff
        SomeModel(some_field="Hello").save()
        # do other stuff
    

    Time line of your code will be like so:

    • Do some stuff
    • Do SomeModel(some_field="Hello").save() call:
      • Do some django stuff
      • Execute signal before_save handler just before actual save
      • Do actual save to DB
    • Do other stuff

    Signals work on same instance of python interpreter (in other words in same process of your OS).

    Celery provides asynchronous tasks. That tasks MAY work like django signals (e.g. celery_task(arg1="Hello")). But common case is async calls:

    celery_task.delay(arg1="Hello") 
    

    This is not a simple function call. This function will be executed in other python process (celery worker). And after this call you can decide: do you want to wait for result of this function? or you keep going with your code? or do you want something tricky?

    Celery is very handy in case you want do some background or scheduled tasks like resize images, decode videos, update facebook status, etc.