#StackBounty: #python #django #multithreading #apache #mod-wsgi Django's AppConfig.ready() seems to be called once per thread in Ap…

Bounty: 50

I have the following code in apps.py:

class Pqawv1Config(AppConfig):
    # ...
    def ready(self):
        env_run_main=os.environ.get('RUN_MAIN')
        print('IS_PRODUCTION=%s, RUN_MAIN=%s' % (settings.IS_PRODUCTION, env_run_main))
        if (not settings.IS_PRODUCTION) and (env_run_main != 'true'):
            print('Exiting because detected running in reloader.')
            return

        print('Starting up for PID=%s, argv=%s...' % (os.getpid(), sys.argv))
        # Initialization of a singleton object from a C++ DLL
        # Raise an exception if this library was already initialized in this process

In the evening I restarted the server and it printed the following in the log as expected:

[Sun Sep 15 22:50:34.928549 2019] [wsgi:error] [pid 11792:tid 1176] Starting up for PID=11792, argv=['mod_wsgi']...r

However, in the morning I noticed that something strange has happened. It looks like Apache started a new thread for the web application:

[Mon Sep 16 04:10:41.224464 2019] [wsgi:error] [pid 11792:tid 1160] Starting up for PID=11792, argv=['mod_wsgi']...r

And later:

[Mon Sep 16 07:16:21.028429 2019] [mpm_winnt:error] [pid 11792:tid 2272] AH00326: Server ran out of threads to serve requests. Consider raising the ThreadsPerChild setting

I think it’s not the issue of calling AppConfig.ready() method twice because there were requests to the website in between and they were handled well. It rather looks like Django’s AppConfig.ready() method is called once per worker thread of Apache process. Is this so? In this case, how to run the code once per process, rather than once per thread in Django powered by Apache and mod-wsgi?


Get this bounty!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.