I've read a lot of stuff on scoped_session, thread safety, and sessions,
and i just don't get it.
For me, a session is just a "wrapper" around the actual database behind it.
And databases do not like concurrent updates of the same row within
multiple transactions.
So, it is my understanding that you should never try to change the same row
within multiple threads.
And this is a limitation that is more strict than just having thread safe
sessions.
For example, i think that something like this is unsafe, even though the
session used is thread safe (because item n°3 is present in two threads):
import threading
def worker1():
items = scoped_session.query(Item).filter(Item.id.in_([1,2,3]))
# do some stuff with items
def worker2():
items = scoped_session.query(Item).filter(Train.id.in_([3,4,5]))
# do some stuff with items
threads = []
t1 = threading.Thread(target=worker1)
t2 = threading.Thread(target=worker2)
t1.start()
t2.start()
So, i don't see the point of keeping one session per thread. There is no
harm in sharing the session, as long as the rows behind it are not used by
multiple threads.
I'm a little bit confused and some explanations/examples would be much
appreciated :)
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.