1

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 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.