Shared ORM objects between threads

36 views
Skip to first unread message

Ben Chopson

unread,
Jun 30, 2022, 4:02:23 PM6/30/22
to sqlalchemy
Hi,

I'm troubleshooting some code that uses thread_pool_executor to run a function, passing an ORM entity as an argument. Within the executed function, we are sometimes receiving a "Value Error: generator already executing" when accessing a related entity via a relationship property.

I'm guessing we shouldn't be passing ORM objects to threads, but rather just passing IDs and then querying the full object in the thread function. Does that hunch sound correct?

Jonathan Vanasco

unread,
Jul 5, 2022, 12:51:52 PM7/5/22
to sqlalchemy

> I'm guessing we shouldn't be passing ORM objects to threads, but rather just passing IDs and then querying the full object in the thread function

Correct.

Database Connections and Sessions are not threadsafe, they are thread-local. See https://docs.sqlalchemy.org/en/14/orm/session_basics.html#is-the-session-thread-safe

Consequently, all objects are thread-local.

If you are simply dealing with read-only concepts, you can `.expunge` an object from one session/thread and `.merge` it into another session/thread.  This is often playing with fire though, as you must be prepared to handle situations where the data may have changed as that type of work is not transaction-safe.  See: https://docs.sqlalchemy.org/en/14/orm/session_state_management.html

Ben Chopson

unread,
Jul 5, 2022, 1:20:34 PM7/5/22
to sqlalchemy
Thanks Jonathan, that should help me move forward.
Reply all
Reply to author
Forward
0 new messages