0

I'm working on a simple Django reputation app, based on Actstream justquick/django-activity-stream/ that creates a signal to be used as: rep.send(request.user, action='increase', target=obj, val=10)

However, even with dispatch UID:

# apps.py
from django.apps import AppConfig
from . signals import rep
from . receivers import rep_handler

class DjangoReputationConfig(AppConfig):
    name = 'django_rep'

    def ready(self):
        rep.connect(rep_handler, dispatch_uid='django_rep.models')`

Which I copied from Actstream/apps.py, I cannot get my signals to send only once. I've tried other unique strings and nothing seems to make a difference.

In my __ init__.py have: default_app_config = 'django_rep.apps.DjangoReputationConfig'

Thanks! The repo is here if you want to see the code!

ab11
  • 45
  • 7

1 Answers1

0

I had exactly the same problem. I tried many things, but in the end I had to resort to using an environment variable, like this:

import os
from django.apps import AppConfig

class MyConfig(AppConfig):
    name = "xyz"

    def ready(self):
        if not os.environ.get("ready_called"): 
            # The development server instantiates MyConfig twice; running via gunicorn only once.
            os.environ["ready_called"] = "1" # Use this environment semaphore to prevent starting backgroundtaks more than once.
            import backgroundtasks
            backgroundtasks.install()
Michiel Overtoom
  • 1,609
  • 13
  • 14
  • Thanks - It turns out is because I had the @receiver decorator on the receiver function and also connecting to it in AppConfig - was this your issue?? – ab11 Sep 14 '15 at 14:34
  • Nope, the only thing I do in my AppConfig is starting a background thread for some periodical database tasks. I _do_ use @receiver here and there as a decorator (I'm using allauth), but I don't connect explicitely to it. – Michiel Overtoom Sep 14 '15 at 15:07
  • For others who find themselves here: this happens in the development server because Django boots-up two processes, one is for the actual development server, and the other is to keep track of files change. Both active the `ready` signal. – felipe Jul 07 '22 at 18:11