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.