Also using the cookie-cutter SQLAlchemy in front of Gunicorn.
1) I have autoflush disabled. It doesn’t reduce the mental load—as your example shows, you still need to understand when and why to use flush—and premature flushes are a headache to work around.
2) After a query fails (in this case a deadlock), the session is unusable for the remainder of the request. This is expected behavior. Usually when I’ve encountered an exception like this, it’s because either my exception view or a tween somewhere is trying to run a query. I’m not sure how to overcome this limitation, but I’ve haven’t found it too bad to work around.