SQLAlchemy ORM: safely passing objects between threads without manually reattaching?
Session.merge() is enough and should do what you're after, but even then it gets fiddly with threads. You might want to rethink this.
Pass the primary key(s) to the worker instead of the objects, and then handle object loading and the actual work in the worker itself. No messing around with threading and open/closed sessions that will eventually lead to headaches. Once the workers can deal with the objects separately, you could even move the workers to a separate process (similar to what Celery does).
I'm working on a multithreaded application that uses the SQLAlchemy ORM. It already uses scoped_session with the thread as its scope, but we are having some issues when we pass an ORM object from a worker thread back to the main thread. Since the objects are attached to the worker thread's session, when the worker thread is shut down, we start getting DetachedInstanceErrors on those objects. 
Is there a way I can generically tell the ORM objects to detach/reattach themselves to the correct session as needed? We spawn a new thread whenever we have a slow operation that we don't want locking up our UI, so putting the reattach code in everywhere we spawn a new thread would be a mess. 
I think we also need to be able to clone the ORM object when we spawn the thread, so that we can have one in the main thread and one in the worker thread. I see a "merge" but no "split". Is this possible?