Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def async_receiver(signal, task_decorator=task, **kwargs):
- """
- @param task_decorator : By default, uses celery's task decorator. Others, like
- django-celery-transactions' (https://github.com/chrisdoble/django-celery-transactions)
- can be substituted.
- """
- def _decorator(func):
- def _with_task(created_task):
- def _call_async(*args, **kwargs):
- # It's undocumented, but django sends `signal` as a kwarg to receivers.
- # Signals can't be pickled.
- try:
- del kwargs['signal']
- except KeyError:
- pass
- new_kwargs = {}
- for key, value in kwargs.items():
- if isinstance(value, models.Model):
- new_kwargs[key+'_id'] = value.id
- elif isinstance(value, (bool, str, int, float)):
- new_kwargs[key] = value
- return created_task.delay(*args, **new_kwargs)
- signal.connect(_call_async, **kwargs)
- return _call_async
- return _with_task(task_decorator(func))
- return _decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement