问题
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