None! Can't pickle <class 'sqlalchemy.orm.session.Session'>: it's not the same object as sqlalchemy.orm.session.Session

938 views
Skip to first unread message

Haris Damara

unread,
Mar 15, 2022, 10:34:17 PM3/15/22
to sqlalchemy
Hi,

Please help.

Find issue with error message "None! Can't pickle <class 'sqlalchemy.orm.session.Session'>: it's not the same object as sqlalchemy.orm.session.Session"
while run the sqlalchemy orm query and/or execute syntax in Process (multiprocessing)

Simon King

unread,
Mar 16, 2022, 6:43:11 AM3/16/22
to sqlal...@googlegroups.com
I haven't used the multiprocessing library, but if it uses pickle to
transfer SQLAlchemy objects, it's going to be difficult to make it
work. Objects loaded via the SQLAlchemy ORM hold a references to the
Session that was used to load them, which in turn holds a reference to
a database connection. Pickling the instance therefore tries to pickle
the Session as well, which almost certainly isn't what you want.

Rather than passing ORM instances to worker processes, it would
probably be better to either:

a) Pass a unique identifier for the instance, and have the worker
process reload the instance from the database itself, or
b) Send simpler (non-SQLAlchemy) objects to the worker,

Hope that helps,

Simon
> --
> 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+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sqlalchemy/f98608ed-1eb6-49d3-90c2-642973c990f0n%40googlegroups.com.

Mike Bayer

unread,
Mar 16, 2022, 11:04:46 AM3/16/22
to noreply-spamdigest via sqlalchemy
can confirm, don't try passing Session objects as arguments over multiprocessing.  that's not going to work for many reasons.   new child process should use its own session.
Reply all
Reply to author
Forward
0 new messages