Django发出两次信号

时间:2015-09-14 13:41:47

标签: django signals activity-streams

我正在开发一个简单的Django声誉应用,基于Actstream justquick / django-activity-stream /创建一个信号用作:     rep.send(request.user, action='increase', target=obj, val=10)

但是,即使使用调度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')`

我从Actstream/apps.py复制的,我无法将信号发送一次。我尝试过其他独特的字符串,似乎没有任何区别。

在我的__ init__.py中有: default_app_config = 'django_rep.apps.DjangoReputationConfig'

谢谢!如果你想查看代码,回购邮件是here

1 个答案:

答案 0 :(得分:0)

我有完全相同的问题。我尝试了很多东西,但最后我不得不求助于使用环境变量,如下所示:

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()