how to setup sqlalchemy session in celery tasks with no global variable

前端 未结 1 543
渐次进展
渐次进展 2020-12-31 10:44

Summary: I want to use a sqlalchemy session in celery tasks without having a global variable containing that session.

I am using SQLAlchemy in a project with celery

相关标签:
1条回答
  • 2020-12-31 11:08

    The answer was right under my nose in the official documentation about custom task classes.

    I modified the custom task class that I use for tasks accessing the database:

    class DBTask(Task):
        _session = None
    
        def after_return(self, *args, **kwargs):
            if self._session is not None:
                self._session.remove()
    
        @property
        def session(self):
            if self._session is None:
                _, self._session = _get_engine_session(self.conf['db_uri'],
                                                       verbose=False)
    
            return self._session
    

    I define my tasks this way:

    @app.task(base=DBTask, bind=True)
    def do_stuff_with_db(self, conf, some_arg):
        self.conf = conf
        thing = self.session.query(Thing).filter_by(arg=some_arg).first()
    

    That way, the SQLAlchemy session will only be created once for each celery worker process, and I don't need any global variable.

    This solves the problem with my unit tests, since the SQLAlchemy session setup is now independant from the celery workers.

    0 讨论(0)
提交回复
热议问题