Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 864
Benjamin Smith Max Avatar asked Nov 29 '13 17:11

Benjamin Smith Max


People also ask

What are Celery signals?

Signals allow decoupled applications to receive notifications when certain actions occur elsewhere in the application. Celery ships with many signals that your application can hook into to augment behavior of certain actions.

What are the signals in Django?

Django includes a “signal dispatcher” which helps decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place.

What does Celery do in Django?

django-celery provides Celery integration for Django; Using the Django ORM and cache backend for storing results, autodiscovery of task modules for applications listed in INSTALLED_APPS, and more. Celery is a task queue/job queue based on distributed message passing.

How do you use Celery signals?

signals import task_prerun, task_postrun app = Celery('tasks', broker='redis://localhost:6379/0') @app. task def add(x, y): return x + y @task_prerun. connect(sender=add) def task_prerun_notifier(sender=None, **kwargs): print("From task_prerun_notifier ==> Running just before add() executes") @task_postrun.


1 Answers

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.

like image 134
awnion Avatar answered Sep 19 '22 01:09

awnion