django.dispatch.dispatcher.Signal should be pickleable
|Reported by:||d0ugal||Owned by:||nobody|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
The Signal class contains an instance of threading.Lock which means it can't be pickled without providing pickle with some hints. It should be pickleable as it is passed in the kwargs to signal receivers, the use case that requires all arguments to be pickleable is best demonstrated as follows.
This example uses a celery task as the receiver of a signal to in effect provide "async signals" which would allow for some nice patterns to be developed.
# In a models.py file in any project with celery already configured. from celery.task import task from django.db import models from django.db.models.signals import pre_delete, pre_save class MyModel(models.Model): pass @task(ignore_result=True) def async_post_save(sender, instance, **kwargs): # do something with the instance. pass post_save.connect(async_post_save.delay, sender=MyModel)
I've attached a patch for this which basically implements a very small monkey patch that I am currently using in a project.
from django.dispatch.dispatcher import Signal def reducer(self): return (Signal, (self.providing_args,)) Signal.__reduce__ = reducer
As it happens, I quickly threw together a blog post about this trick: http://dougalmatthews.com/2011/10/10/making-django's-signals-asynchronous-with-celery/