How To Run Arbitrary Code After Django is “Fully Loaded”

六月ゝ 毕业季﹏ 提交于 2019-12-19 05:07:17

问题


I need to perform some fairly simple tasks after my Django environment has been "fully loaded".

More specifically I need to do things like Signal.disconnect() some Django Signals that are setup by my third party library by default and connect my own Signals and I need to do some "monkey patching" to add convenience functions to some Django models from another library.

I've been doing this stuff in my Django app's __init__.py file, which seems to work fine for the monkey patching, but doesn't work for my Signal disconnecting. The problem appears to be one of timing--for whatever reason the Third Party Library always seems to call its Signal.connect() after I try to Signal.disconnect() it.

So two questions:

Do I have any guarantee based on the order of my INSTALLED_APPS the order of when my app's __init__.py module is loaded?

Is there a proper place to put logic that needs to run after Django apps have been fully loaded into memory?


回答1:


In Django 1.7 Apps can implement the ready() method: https://docs.djangoproject.com/en/dev/ref/applications/#django.apps.AppConfig.ready




回答2:


My question is a more poorly phrased duplicate of this question: Where To Put Django Startup Code. The answer comes from that question:

Write middleware that does this in init and afterwards raise django.core.exceptions.MiddlewareNotUsed from the init, django will remove it for all requests...

See the Django documentation on writing your own middleware.




回答3:


I had to do the following monkey patching. I use django 1.5 from github branch. I don't know if that's the proper way to do it, but it works for me.

I couldn't use middleware, because i also wanted the manage.py scripts to be affected.

anyway, here's this rather simple patch:

import django
from django.db.models.loading import AppCache

django_apps_loaded = django.dispatch.Signal()

def populate_with_signal(cls):
    ret = cls._populate_orig()
    if cls.app_cache_ready():
        if not hasattr(cls, '__signal_sent'):
            cls.__signal_sent = True
            django_apps_loaded.send(sender=None)
    return ret

if not hasattr(AppCache, '_populate_orig'):
    AppCache._populate_orig = AppCache._populate
    AppCache._populate = populate_with_signal

and then you could use this signal like any other:

def django_apps_loaded_receiver(sender, *args, **kwargs):
    # put your code here.
django_apps_loaded.connect(django_apps_loaded_receiver)



回答4:


As far as I know there's no such thing as "fully loaded". Plenty of Django functions include import something right in the function. Those imports will only happen if you actually invoke that function. The only way to do what you want would be to explicitly import the things you want to patch (which you should be able to do anywhere) and then patch them. Thereafter any other imports will re-use them.



来源:https://stackoverflow.com/questions/5439650/how-to-run-arbitrary-code-after-django-is-fully-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!