[Question] Using vmprof with uwsgi
I'd like to know if there is any way to use vmprof to profile a python web app in a production style environment.
Current problem
I'd like to profile a flask app running with uwsgi.
Let's assume I have the following project directory:
.
├── app.py
├── uwsgi.ini
└── wsgi.py
And my wsgi.py module
from app import app_factory
app = app_factory('app.settings.defaults.Config')
I tried doing something like this to manually start/stop the profiler
import atexit
import os
import vmprof
from app import app_factory
fp = open("/tmp/vmprof.out-%s" % os.getpid(), "w")
vmprof.enable(fp.fileno(), warn=False)
app = app_factory('app.settings.defaults.Config')
def disable_at_exit():
vmprof.disable()
fp.close()
atexit.register(disable_at_exit)
And ran wsgi with this command uwsgi -http :8080 --wsgi-file uwsgi.py
Unfortunately uwsgi just returns 500 when performing any http request on localhost:8080.
Is the only way to run vmprof to call it through it's entry point like this python -m vmprof <module_to_be_profiled>?
Tried to reproduce your setup with python3.6. As you described it the uwsgi app could not hinge the application. The following worked:
app.py
import atexit
import os
import vmprof
# I removed the import of app_factory here flask 0.12 does not seem to export it
fp = open("/tmp/vmprof.out-%s" % os.getpid(), "a+") # only 'r' does not work, must be writable as well
vmprof.enable(fp.fileno())
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
def disable_at_exit():
vmprof.disable()
fp.close()
atexit.register(disable_at_exit)
Then mount the application on the mountpoint: /yourapplication
uwsgi --http :8080 --wsgi-file uwsgi.py --mount /yourapplication=app:app
If that does not work for you I need more information to reproduce.
AFAIK, vmprof uses SIGPROF. This option is required, maybe. http://uwsgi-docs.readthedocs.io/en/latest/Options.html#py-call-osafterfork