Opened 5 years ago

Closed 5 years ago

#17029 closed Bug (wontfix)

django.dispatch.dispatcher.Signal should be pickleable

Reported by: Dougal Matthews Owned by: nobody
Component: Core (Other) Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: 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 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):

def async_post_save(sender, instance, **kwargs):
    # do something with the instance.

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:'s-signals-asynchronous-with-celery/

Attachments (1)

pickled_signal.diff (1.8 KB) - added by Dougal Matthews 5 years ago.

Download all attachments as: .zip

Change History (3)

Changed 5 years ago by Dougal Matthews

Attachment: pickled_signal.diff added

comment:1 Changed 5 years ago by Greg Taylor

Needs documentation: unset
Needs tests: unset
Patch needs improvement: unset

Very neat idea. We'd like this very much.

comment:2 Changed 5 years ago by Alex Gaynor

Resolution: wontfix
Status: newclosed

After speaking with Carl, I'm marking this as wontfix because it is non-obvious as to whether pickling a Signal should include the registered receivers, and how that interacts with the weak referencing, since there's no obvious semantic it seems better not to guess.

Note: See TracTickets for help on using tickets.
Back to Top