mpp.py 1.24 KB
import sismiop.services.base
from zope.sqlalchemy import register
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import engine_from_config
import logging
# Will be set by main process using multiprocessing.Manager().dict()
mpp_connections = None
request_queue = None
response_queue = None


DBSession = scoped_session(sessionmaker())
register(DBSession)


def get_connection(config):
    # print(f"[get_connection] Received config: {config}")
    db_url = config.get('other_db_url', None)

    if not db_url:
        print(
            "Database URL not found in ini file under [main] section with key 'db_url'.")
        raise ValueError(
            "Database URL not found in ini file under [main] section with key 'db_url'.")

    module_name = config.get('module', None)
    if not module_name:
        print(
            "Module name not found in ini file under [main] section with key 'module'.")
        raise ValueError(
            "Module name not found in ini file under [main] section with key 'module'.")

    engine = engine_from_config(config, prefix='other_db_')
    DBSession.configure(bind=engine)
    # print(f"[get_connection] DBSession.bind after configure: {DBSession.bind}")
    sismiop.services.base.DBSession = DBSession