[sqlalchemy] Re: Shared ORM objects between threads

2022-07-05 Thread 'Ben Chopson' via sqlalchemy
Thanks Jonathan, that should help me move forward.

On Tuesday, July 5, 2022 at 12:51:52 PM UTC-4 Jonathan Vanasco wrote:

>
> > 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
>
>
>
> On Thursday, June 30, 2022 at 4:02:23 PM UTC-4 ben.c...@level12.io wrote:
>
>> 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?
>>
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/b1c62fc8-7fd6-449e-ae65-2bc3cc22e1b7n%40googlegroups.com.


[sqlalchemy] Re: Shared ORM objects between threads

2022-07-05 Thread 'Jonathan Vanasco' via 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



On Thursday, June 30, 2022 at 4:02:23 PM UTC-4 ben.c...@level12.io wrote:

> 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?
>

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/b4fe09b4-bca9-43c6-9079-e601d88100e5n%40googlegroups.com.