<commit_before>import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from celery import conf

ResultModelBase = declarative_base()

_SETUP = {"results": False}


def create_session(dburi, **kwargs):
    engine = create_engine(dburi, **kwargs)
    return engine, sessionmaker(bind=engine)


def setup_results(engine):
    if not _SETUP["results"]:
        ResultModelBase.metadata.create_all(engine)
        _SETUP["results"] = True


def ResultSession(dburi=conf.RESULT_DBURI, **kwargs):
    engine, session = create_session(dburi, **kwargs)
    if os.environ.get("CELERYINIT"):
        setup_results(engine)
    return session()
<commit_msg>celery.db.session: Only one engine per dburi<commit_after>import os

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from celery import conf

ResultModelBase = declarative_base()

_SETUP = {"results": False}
_ENGINES = {}


def get_engine(dburi, **kwargs):
    if dburi not in _ENGINES:
        _ENGINES[dburi] = create_engine(dburi, **kwargs)
    return _ENGINES[dburi]


def create_session(dburi, **kwargs):
    engine = get_engine(dburi, **kwargs)
    return engine, sessionmaker(bind=engine)


def setup_results(engine):
    if not _SETUP["results"]:
        ResultModelBase.metadata.create_all(engine)
        _SETUP["results"] = True


def ResultSession(dburi=conf.RESULT_DBURI, **kwargs):
    engine, session = create_session(dburi, **kwargs)
    if os.environ.get("CELERYINIT"):
        setup_results(engine)
    return session()

{'fraction_non_alphanumeric': 0.1031985515992758, 'fraction_numerical': 0.0, 'mean_word_length': 4.737024221453288, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7350113', 'n_tokens_mistral': 532, 'n_tokens_neox': 520, 'n_words': 123}