How to avoid AppConfig.ready() method running twice in Django

前端 未结 4 1680
野性不改
野性不改 2020-12-05 06:32

I want to execute some code at startup of Django server but I want it to run only once. Currently when I start the server it\'s executed twice. Documentation says that this

相关标签:
4条回答
  • 2020-12-05 06:45

    As Roberto mentioned you will need to implement locking in order to do this when running your server through the runserver command, if you want to use the default auto_reload functionality.

    Django implements it's auto_reload via threading and so imports the AppConfig in two separate threads, the main 'command/watch' thread and the 'reload' thread running the server. Add a print statement to the module and you will see this in action. The 'main' thread loads the AppConfig files as part of it's BaseCommand execute, and the 'reload' thread then load them again during it's startup of the server.

    If you have code that can not be run in both of these threads then your options are somewhat limited. You can implement a thread lock so that the 'reload' thread will not run ready(); you can move to a production environment to run your server (Gunicorn for example is very quick to setup, even for testing); or you can call your method in another way rather than using ready().

    I would advise moving to a proper environment, but the best option really depends on what exactly the method you are calling is supposed to do.

    0 讨论(0)
  • 2020-12-05 06:46

    if you don't want to use --noreload you can:

    replace the line in your app's __init__.py that you use to specify the config:

    default_app_config = 'mydjangoapp.apps.MydjangoappConfig'
    

    by this:

    import os
    
    if os.environ.get('RUN_MAIN', None) != 'true':
        default_app_config = 'mydjangoapp.apps.MydjangoappConfig'
    
    0 讨论(0)
  • 2020-12-05 06:48

    When you use python manage.py runserver Django start two processes, one for the actual development server and other to reload your application when the code change.

    You can also start the server without the reload option, and you will see only one process running will only be executed once :

    python manage.py runserver --noreload
    

    You can see this link, it resolves the ready() method running twice in Django

    0 讨论(0)
  • 2020-12-05 06:55

    You need to implement locking. It is not a simple problem and the solution will not feel natural as you are dealing with processes and threads. Be warned there are many answers to the problem of locking, some simpler approaches:

    A file lock: Ensure a single instance of an application in Linux (note that threads share file lock by default so this answer needs to be expanded to account for threads).

    There is also this answer which uses a Python package called tendo that encapsulates the a file lock implementation: https://stackoverflow.com/a/1265445/181907

    Django itself provides an abstracted portable file locking utility in django.core.files.locks.

    0 讨论(0)
提交回复
热议问题