7

I am scratching my head on this one.

I have a Flask App w/ Flask-Cache and SqlAlchemy:

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql+pg8000://[Redacted]
cache = Cache(app, config={'CACHE_TYPE':'redis', 'CACHE_REDIS_URL':'[Redacted]'})
db = SQLAlchemy(app)

Celery workers:

@worker_process_init.connect
def init_worker(**kwargs):
    global db_session
    print('Initializing database connection for worker.')
    db_session = database.get_session()


@worker_process_shutdown.connect
def shutdown_worker(**kwargs):
    global db_session
    if db_session.is_active:
        print('Closing database connectionn for worker.')
        db_session.close()

And a general-purpose get_or_create def:

@cache.cached(timeout=200, key_prefix="get_or_create")
def get_or_create(model, **kwargs):
    instance = model.query.filter_by(**kwargs).first()
    if instance:
        return instance
    else:
        instance = model(**kwargs)
        db_session.add(instance)
        return instance

I'm trying to use the cache to resolve the multiprocessing from causing UniqueConstraint violations (ie when two of the workers insert non-unique objects at the same time, when one should be updating after the first one inserts)

The workers are spewing

InvalidRequestError: Instance '<[Redacted]>' is not persistent within this Session

Best I can figure out is that I need to expand the session's scope?

Patches
  • 1,423
  • 2
  • 12
  • 11
  • 2
    For years past, still no answer for it. I find this question, but no answer. Oops~ – Light.G Mar 19 '19 at 03:13
  • Not sure what's going on, this exception is thrown from `Session._validate_persistent` in `/sqlalchemy/orm/session.py`. Try debugging there directly, if you cannot gain any info try posting this question to issues of sqlalchemy github repo. – cya Dec 03 '22 at 13:57

0 Answers0