Thank you Jonathan,

This is how I create both engine/session pairs:

from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
import zope.sqlalchemy

class DBTask(app.task)
    _engine = None
    _domain_model_session = None

    def checkpoint(self, label):
        *# celery_state_session and engine*
        settings = self.app.conf["PYRAMID_REGISTRY"].settings
        eng = engine_from_config(settings, prefix='sqlalchemy.')
         # eng.update_execution_options(autocommit=True, autoflush=False)
        factory = sessionmaker()
        factory.configure(bind=eng)
        celery_state_session = factory()
        celery_state_session.query(...)
        ...
        celery_state_session.flush()
        celery_state_session.commit()

    @property
    def engine(self):
        *# domain_model_session's engine*
        if self._engine is None:
            settings = self.app.conf["PYRAMID_REGISTRY"].settings
            self._engine = engine_from_config(settings, 
prefix='sqlalchemy.')
        return self._engine

    @property
    def domain_model_session(self):
       * # domain_model_session*
        if self._domain_model_session is None:
            factory = sessionmaker()
            factory.configure(bind=self.engine)
            self._domain_model_session = factory()
            zope.sqlalchemy.register(self._domain_model_session, 
transaction_manager=transaction.manager)
        return self._domain_model_session

@app.task(base=DBTask, bind=True)
def celery_task(self, ...):
    self.checkpoint('started')
    try:
        with transaction.manager:
            self.domain_model_session.query(...)
            self.checkpoint('did something')

    except Exception as e:
        self.checkpoint('failed')

    finally:
        self.checkpoint('done')

I will test the `domain_model_session` without zope.sqlalchemy to see if 
that affects the `celery_state_session`. I have been wondering about 
psycopg2 also.

Again, thanks!

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to