Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django signal emitting once, received twice -- Why?

I'm working with Django signals, but they seem to be received twice, even if emitted once. Here's the code I'm working with (it's a simple wrapper to use Uploadify with Django)...

# Signal-emitting code... emits whenever a file upload is received
# ----------------------------------------------------------------
upload_recieved = django.dispatch.Signal(providing_args=['data'])

def upload(request, *args, **kwargs):
    if request.method == 'POST':
        if request.FILES:
            print 'sending signal'
            upload_recieved.send(sender='uploadify', data=request.FILES['Filedata'])
    return HttpResponse('True')

# Signal-receiving code...
# ----------------------------------------------------------------    
def upload_received_handler(sender, data, **kwargs):
    print 'upload received handler'

print 'connecting signal'
upload_recieved.connect(upload_received_handler)

(I just noticed my signal is spelled wrong)

I'm sure you noticed the print statements in there. On the console, this is what it's showing:

(server starts)
connecting signal

...

sending signal
upload received handler
upload received handler     # << == where is this 2nd one coming from?
127.0.0.1 - - [25/Sep/2009 07:28:22] "POST /uploadify/upload/ HTTP/1.1" 200 -

(also odd is why does Django report the page POST after the signals are fired?)

like image 719
T. Stone Avatar asked Sep 25 '09 17:09

T. Stone


People also ask

How does signal work 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.

Are Django signals asynchronous?

Async Signals provides an implementation of Django signals that are fired asynchronously. When you send a Django signal, all receivers are called synchronously. Sometimes you want to just send the signal, and move on.

What is the use of the Post_delete signal in Django?

To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.

What kind of signals are there in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown. pre_init/post_init: This signal is thrown before/after instantiating a model (__init__() method).


1 Answers

This has happened to me before and it was due to the module where you are connecting the signal being imported twice. To make sure the signal isn't connected twice you can set the dispatch_uid:

upload_recieved.connect(upload_received_handler, dispatch_uid="some.unique.string.id")

UPDATE It is actually documented here: http://code.djangoproject.com/wiki/Signals#Helppost_saveseemstobeemittedtwiceforeachsave

like image 53
Ricky Avatar answered Sep 19 '22 08:09

Ricky