Our application uses gunicorn and we are looking to enable preload_app. In 
preparation, we moved the creation of the sqlalchemy engine into a 
post-fork hook for each gunicorn worker, ensuring that DB connections are 
created separately within each process.

Unfortunately our sqlalchemy declarative base class, which is imperative 
during app preload for import dependency reasons, needs a MetaData object 
with specific naming conventions. Because MetaData requires an engine, we 
initially create an engine, instantiate the MetaData object, then after 
application preload, dispose of the engine’s connection. After this, the 
post-fork hook kicks in and all processes’ engines and sessions are created.


Our question is: Could this implementation become problematic, and how 
might we test it?



Preload:

from sqlalchemy import create_enginefrom sqlalchemy.ext.declarative import 
declarative_baseengine = create_engine(conn_string)metadata = MetaData(engine, 
naming_convention=convention)Base = declarative_base(cls=Base, 
metadata=metadata)# Multiple model classes inherit from Base



After preload:


engine.dispose()



Post-fork hooks:


# Runs once for every process# All model classes are still based on the 
original declarative_base metadataengine = create_engine(conn_string)# Session 
is created from engine


Thank you.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/6c892370-1f0e-4e05-98ab-5f256b21fb0d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to