From what I understand, Apache mod_wsgi implements a python interpreter that runs alongside Apache and serves CGI requests, but the difference between WSGI and CGI is that, with WSGI the Python session just keeps running as long as the Apache server is running whereas with CGI it has to restart every time the server gets a CGI request.
What I’d like to know is, is there a Python module that you can run separately from Apache, like in an interactive Python session, that will listen for CGI requests from Apache? So, for instance, you can have Apache running and your WSGI middleware not running, and then you can start an interactive Python session and import your WSGI middleware module and then it will serve CGI requests from Apache, and you can also shut it down without shutting down Apache. So it is a single session like Apache mod_wsgi, but it doesn’t have to always run concurrently with Apache, and you can run it from an interactive Python session.
Edit 1:
So for instance, I have this flask app, myapp.py:
from flask import Flask
app = Flask(__name__)
app.debug = True
app.apples = 0
@app.route('/')
def hello():
app.apples += 1
return 'blah: %d' % app.apples
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8080)
And I can run it by typing python myapp.py, but it starts its own web server. Instead I’d like Apache to be the web server, but I’d like to be able to open an interactive python shell and type from myapp import * and have app listen for requests from Apache, and since I have my interactive shell, so I can do stuff like print app.apples or app.apples = 50 or whatever. I’m saying I want my web app to be separate from the web server, and to be interactive with the python shell.
And what I mean to say with app.apples is that it is a global variable, like a hit count for the number of times '/' has been accessed, and it persists as long as the app is running.
Edit 2:
Here’s another example.
myapp.py:
import web
def add_global_hook():
g = web.storage({"counter": 0})
def _wrapper(handler):
web.ctx.globals = g
return handler()
return _wrapper
class Hello:
def GET(self):
web.ctx.globals.counter += 1
return "<h1>Counter: %d</h1>" % web.ctx.globals.counter
urls = ("/", "Hello")
app = web.application(urls, globals())
app.add_processor(add_global_hook())
app.run()
Here, I can open a python interpreter and type from myapp import *, and it starts the web server, but while the web server is running, I’m blocked from using the interactive shell. Is there some way to run the server in a non-blocking way so that I can use the interactive shell?
Have your considered running gunicorn WSGI server with Apache as a front end proxy?
If you are after ability to debug a live Python web application without needing to do anything too complicated, also look at:
https://github.com/GrahamDumpleton/wsgi-shell
You would just want to make sure you use mod_wsgi daemon mode within default of single daemon process if using mod_wsgi.
BTW, your understanding of how mod_wsgi works is a bit wrong, but rather than try and correct that it would simply help that you explain better why you want to do what you are trying to do. Perhaps starting by presenting the actual problem rather than your perceived solution.