Thursday, January 26, 2017

SQLAlchemy - Unexpected Lazy Loading

If you're seeing unexpected lazy loading on a lazy="joined" relationship in SQLAlchemy, it might be because you're accessing those relationships after you've already run session.commit(). By default, session.commit() will expire the data on your relationships, meaning it will try to fetch it again next time you try to access those attributes.

The relevant section of the docs for session.commit():
By default, the Session also expires all database loaded state on all ORM-managed attributes after transaction commit. This so that subsequent operations load the most recent data from the database. This behavior can be disabled using the expire_on_commit=False option to sessionmaker or the Session constructor.


Thursday, January 19, 2017

Python - David Beazley's "Understanding The Python GIL"

This talk explains a lot about Python's multithreading: http://www.dabeaz.com/python/UnderstandingGIL.pdf

In Python 2.7, it knows to switch to different thread whenever it starts waiting on IO. And, it will check if it needs to switch threads every 100 bytecode instructions.

Monday, January 16, 2017

SQLAlchemy - Is is necessary to commit after session.execute?

The docs for session.execute say:
Execute a SQL expression construct or string statement within the current transaction.
So, the answer is yes, you need to issue a commit after running session.execute().

Here's a code example along with the output from "echo=True" showing it begins the session but doesn't end it unless you run session.commit():