librelist archives

« back to archive

Deploying to a WSGI container

Deploying to a WSGI container

From:
Adam Davis
Date:
2012-09-06 @ 23:01
I have been developing a flask application using the included server. I
am now trying to deploy using mod_wsgi with Apache but I am having
trouble understanding how some of the basic configuration is done.

When running under the flask development server, I have a run.py file,
as suggested in the docs:

	from my_app import app
	from flaskext.breve import Breve
	from my_app.database import init_db, session
	
	if __name__ == '__main__':

	    	breve = Breve(app)
    		init_db(app.config['DATABASE_URI'])

		app.run(host=app.config['HOST'],
            	debug=app.config['DEBUG'],
            	port=app.config['PORT'])

... and a simple __init__.py for my application:

	from flask import Flask
	app = Flask('my_app')

	app.config.from_envvar('MYAPP_SETTINGS')

	breve = Breve(app)
	init_db(app.config['DATABASE_URI'])

	import view_rating.views
	import view_rating.database
	import view_rating.model

As a WSGI application, I understand that I should just need to:

	import app as application

... yet it's not clear to me how I then pass the app to the breve
extension and initialize the database, as my original run.py was doing.
I have tried an application factory function within __init__.py, as I
thought perhaps the configuration needed to occur prior to import in the
wsgi module. But with all my experiments I keep getting the same result:
SQLAlchemy, called from some of my controller code, can't find a mapper
for one of my objects. I assume that the call to init_db() isn't
happening correctly.

Can anyone point me to a snippet showing how I would do such
initialization (both for extensions and database sessions) in the
context of a WSGI application?

-Adam

Re: [flask] Deploying to a WSGI container

From:
Audrius Kažukauskas
Date:
2012-09-07 @ 09:07
On Fri, 2012-09-07 at 00:01:50 +0100, Adam Davis wrote:
> When running under the flask development server, I have a run.py file,
> as suggested in the docs:
> 
> 	from my_app import app
> 	from flaskext.breve import Breve
> 	from my_app.database import init_db, session
> 	
> 	if __name__ == '__main__':
> 
> 	    	breve = Breve(app)
>     		init_db(app.config['DATABASE_URI'])
> 
> 		app.run(host=app.config['HOST'],
>             	debug=app.config['DEBUG'],
>             	port=app.config['PORT'])
> 
> ... and a simple __init__.py for my application:
> 
> 	from flask import Flask
> 	app = Flask('my_app')
> 
> 	app.config.from_envvar('MYAPP_SETTINGS')
> 
> 	breve = Breve(app)
> 	init_db(app.config['DATABASE_URI'])
> 
> 	import view_rating.views
> 	import view_rating.database
> 	import view_rating.model
> 
> As a WSGI application, I understand that I should just need to:
> 
> 	import app as application
> 
> ... yet it's not clear to me how I then pass the app to the breve
> extension and initialize the database, as my original run.py was doing.

Well, aren't you already doing this inside __init__.py?  Right now it
looks like you're registering an extension and calling init_db twice: in
run.py and __init__.py.  Or am I missing something?

If webapp initialization is being done in __init__.py, then you are
correct that

  from my_app import app as application

should be enough for mod_wsgi.

> I have tried an application factory function within __init__.py, as I
> thought perhaps the configuration needed to occur prior to import in the
> wsgi module. But with all my experiments I keep getting the same result:
> SQLAlchemy, called from some of my controller code, can't find a mapper
> for one of my objects. I assume that the call to init_db() isn't
> happening correctly.

Without the traceback it's hard to say what is the actual problem.

-- 
Audrius Kažukauskas
http://neutrino.lt/