Easy WSGI reset for Python/Django/Wagtail hosted websites

#1
If your OpenLiteSpeed setup is pointed to the wsgi.py file in python it can be confusing when changes made in your local python app won't update as expected when changes are pulled to your website or pushed. Graceful (from OLS Backend), Soft & Hard (from Linux command line) OLS server resets won't always result in old wsgi processes being stopped and new ones created. Often I've had to completely reboot the server to ensure changes are made.

This is especially perplexing as these changes can brick your setup resulting in 400 and 500 errors that are difficult to troubleshoot because of the wsgi structure. A common instance for this to occur is when the settings.py file is modified. Locally "python manage.py runserver" and even a locally hosted wsgi test can show your changes working correctly, but the server still won't function as expected.

The solution I've found is to search for and kill wsgi processes that can be identified by the location of your wsgi.py file:

Running this in bash will help you identify the wsgy processes that need killed:
Code:
ps aux | grep -E '^lsadm.*/project\/app\/wsgi.py'
A tool to monitor processes running on your Linux system:
Code:
ps aux
regex formatted search string:
Code:
grep -E
identifies processes owned by lsadm (OLS admin server) in the folder structure /project/app/wsgi.py:
Code:
^lsadm.*/project\/app\/wsgi.py
You'll need to adjust the project/app/wsgi.py folder scheme to match the pattern where you wsgi.py file is located. The server will initiate wsgi.py processes as needed to host your website. To kill these processes, identify the process number (1234, will never be the same) and then run:
Code:
sudo kill 1234
For a one-line kill all processes run this after you have modified project/app to match the location of your wsgi.py file
Code:
sudo kill $(ps aux | grep -E '^lsadm.*/project\/app\/wsgi.py' | awk '{print $2}')
The nice thing about OLS is on the next server request to your website, new wsgi.py processes will be automatically be fired upon request after the old wsgi.py processes are killed. You won't have to soft or hard reset to see your changes.

Here is some additional information you may find useful concerning this issue:
modwsgi.readthedocs.io/en/master/user-guides/reloading-source-code.html

From what I can tell OLS runs in EMBEDDED MODE and isn't able to rest easily without killing these processes. Maybe someone has a better procedure?
 
Last edited:

Cold-Egg

Administrator
#2
Thanks for sharing, OpenLiteSpeed comes with python in detached mode by default, so you will need to restart python with kill command to make any new settings take effect.
 
#3
Thanks for sharing, OpenLiteSpeed comes with python in detached mode by default, so you will need to restart python with kill command to make any new settings take effect.
I ran a version of the following code to confirm my setup was in Embedded:

https://modwsgi.readthedocs.io/en/master/user-guides/reloading-source-code.html
Code:
def application(environ, start_response):
    status = '200 OK'

    if not environ['mod_wsgi.process_group']:
      output = u'EMBEDDED MODE'
    else:
      output = u'DAEMON MODE'

    response_headers = [('Content-Type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

    start_response(status, response_headers)

    return [output.encode('UTF-8')]
I don't recall changing the default mode, but perhaps I did - can you kindly point me towards the setting to switch back to detached mode instead of embedded?
 
Top