I am having an issue with SQLAlchemy in Celery tasks. I have one task that creates some objects, commits them to the database and then it adds another task with the IDs of these new database rows and the other task then fetches and works on those. However, the other task is not able to get the rows even though they are in the database.
To sketch it up:
@task(name="create-task")
def create_task():
obj = SomeClass(column1="Something", column2="Else")
session.add(obj)
session.commit()
consume_task.delay(obj.id)
@task(name="consume-task")
def consume_task(obj_id):
obj = session.query(SomeClass).get(obj_id)
# obj is None
These two tasks are run in two separate celery processes which initialises their own connection and session in the worker_process_init.connect
signal, but the session on the consume_task apparently is not able to fetch the rows from the database.
If I start the consume_task celery process after the create_task, then it works for those rows inserted before it was started. But I need them to run simultaneously.
Am I missing something completely obvious here about session behaviour? I thought that a SQLAlchemy session query would always query the actual database and not on some "snapshot" from when it was instanced."