I’m working on writing a small WSGI application using Bottle and SqlAlchemy and am confused on how the “layout” of my application should be in terms of SqlAlchemy.
My confusion is with creating engines and sessions. My understanding is that I should only create one engine with the ‘create_engine’ method. Should I be creating an engine instance in the global namespace in some sort of singleton pattern and creating sessions based off of it? How have you done this in your projects?
Any insight would be appreciated. The examples in the documentation dont seem to make this entirely clear (unless I’m missing something obvious). Any thoughts?
What you need to achieve is well described in the pylons documentation: Defining Tables and ORM classes:
The example of the
__init__.pyis shown in the link, whereas themeta.pylooks similar to this:You can consider this module a
singletonimplementation if you like, since it will do the job (of loading and having one instance in more Pythonic) for you when you first load the module.