We have a server that is configured to run a pyramid+sqlalchemy app with modwsgi+apache2
We have a few things in the __init__.py of the pyramid app to create database and prepopulate some test users and accounts. It is similar to the initialize_sql function in pyramid cookbook example here
Out apache config looks something like this (Copied from somewhere and hacked):
WSGIApplicationGroup %{GLOBAL}
WSGIPassAuthorization On
WSGIDaemonProcess pyramid user=ubuntu group=staff processes=1 \
threads=4 \
python-path= VIRTUAL_ENV_SITE_PACKAGES
WSGIScriptAlias / WSGI_SCRIPT_PATH
<Directory /Users/chrism/modwsgi/env>
WSGIProcessGroup pyramid
Order allow,deny
Allow from all
</Directory>
Whats been bothering us is that the initialize_function gets called on many requests instead of just being called once. We do not understand how apache works or what we have done in the config. We want to be able to call the functions in __init__.py once and thats it. Can someone explain how apache actually works and why pyramid __init__.py is being reloaded all the time. Also explain how we can make sure that the __init__.py is not rerun every request
Consider sharing simple to understand links regarding the same stuff 🙂
Things like creating a database and prepopulating it shouldn’t go into the
__init__.pyfile of a WSGI application because, as you’ve noticed, this file gets run whenever you start your server (and at certain other points as well).What you need is a script which can be called from the command line whenever you need to. Every framework provides their own way to build these. In Pyramid they are called
console scripts. Here you can find the documentation of how to build one yourself.However, I recommend you use the scaffold (project template) from this tutorial and have a look at the console script called
initialize_tutorial_db. It does exactly what you want and allows you to create / initialize your database from the command line whenever you need to.Regarding the apache configuration, the line which is relevant to your question is the following:
It means you will start 1 process with 4 threads. This shouldn’t mean that the code in your
__init__.pyfile is run multiple times. That is more likely to be caused from you restarting the server manually or through code changes.