Search code examples
pythonsqlalchemycelery

SQLAlchemy after_insert triggering celery tasks


I'm initiating celery tasks via after_insert events.

Some of the celery tasks end up updating the db and therefore need the id of the newly inserted row. This is quite error-prone because it appears that if the celery task starts running immediately sometimes sqlalchemy will not have finished committing to the db and celery won't find the row.

What are my other options?

I guess I could gather these celery tasks up somehow and only send them on "after_commit" but it feels unnecessarily complicated.


Solution

  • It wasn't so complicated, subclass Session, providing a list for appending tasks via after_insert. Then run through the list in after_commit.