I know it's not recommended to run a Bottle or Flask app on production with python myapp.py --port=80
because it's a development server only.
I think it's not recommended as well to run it with python myapp.py --port=5000
and link it to Apache with: RewriteEngine On
, RewriteRule /(.*) http://localhost:5000/$1 [P,L]
(or am I wrong?), because WSGI is preferred.
So I'm currently setting up Python app <-> mod_wsgi <-> Apache
(without gunicorn or other tool to keep things simple).
Question: when using WSGI, I know it's Apache and mod_wsgi
that will automatically start/stop enough processes running myapp.py
when requests will come, but:
- how can I manually stop these processes?
- more generally, is there a way to monitor them / know how many processes started by mod_wsgi are currently still running? (one reason, among others, is to check if the processes terminate after a request or if they stay running)
Example:
I made some changes in
myapp.py
, and I want to restart all processes running it, that have been launched by mod_wsgi (Note: I know that mod_wsgi can watch changes on the source code, and relaunch, but this only works on changes made on the .wsgi file, not on the .py file. I already read thattouch myapp.wsgi
can be a solution for that, but more generally I'd like to be able to stop and restart manually)I want to temporarily stop the whole application
myapp.py
(all instances of it)
I don't want to use service apache2 stop
for that because I also run other websites with Apache, not just this one (I have a few VirtualHosts
). For the same reason (I run other websites with Apache, and some client might be downloading a 1 GB file at the same time), I don't want to do service apache2 restart
that would have an effect on all websites using Apache.
I'm looking for a cleaner way than kill pid
or SIGTERM, etc. (because I read it's not recommended to use signals in this case).
Note: I already read How to do graceful application shutdown from mod_wsgi, it helped, but here it's complementary questions, not a duplicate.
My current Python Bottle + Apache + mod_wsgi setup:
Installation:
apt-get install libapache2-mod-wsgi a2enmod wsgi # might be done automatically by previous line, but just to be sure
Apache config (source: Bottle doc; a more simple config can be found here):
<VirtualHost *:80> ServerName example.com WSGIDaemonProcess yourapp user=www-data group=www-data processes=5 threads=5 WSGIScriptAlias / /home/www/wsgi_test/app.wsgi <Directory /> Require all granted </Directory> </VirtualHost>
There should be up to 5 processes, is that right? As stated before in the question, how to know how many are running, how to stop them?
/home/www/wsgi_test/app.wsgi
(source: Bottle doc)import os from bottle import route, template, default_app os.chdir(os.path.dirname(__file__)) @route('/hello/<name>') def index(name): return template('<b>Hello {{name}}</b>!', name=name) application = default_app()