SQLAlchemy session handling in delayed Celery tasks

故事扮演 提交于 2019-12-11 13:58:08

问题


I am using a relational database via SQLAlchemy. I want to spawn a job that deals with databases using Celery. There is a code:

from sqlalchemy.orm.session import Session
from celery.task import task
from myapp.user import User

@task
def job(user):
    # job...
    session = Session.object_session(user)
    with user.begin():
        user.value = result_value

def ordinary_web_request_handler(uid):
    assert isinstance(session, Session)
    user = session.query(User).get(int(uid))
    # deals with user...
    job.delay(user)
    return response

I need to use a SQLAlchemy session in the delayed job, but there’s no session yet. How can I set a session into passed user entity?

There are ways I thought, but I am not sure which of these (or none of these) is the best practice:

  • Always pass only primary keys and retrieve new instances by the passed primary keys.
  • Set the session of the passed instances (but I don’t know how).
  • Do not use ORM in the delayed task.

回答1:


You should be able to initiate your session with the worker signals: http://celery.readthedocs.org/en/latest/userguide/signals.html#worker-signals

If you use a singleton-like pattern to make sure you always have a session available in that thread than everything should work just fine.



来源:https://stackoverflow.com/questions/8034098/sqlalchemy-session-handling-in-delayed-celery-tasks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!