A problem that can arise when using sqla connection pooling is (I'm not
sure that this happens under all conditions, but it has happened to me):
1. In the master process (e.g. in pyramid main()), engine is set up
(with pooling) a sqlconnection made, used, and closed. The connection,
in this case is not actually closed, but just returned to the pool.
2. Fork (e.g. uwsgi forks the workers)
3. Each child process thinks it's got a connection in its pool. The
problem is it's the same connection in each process. When multiple
process try to use the same connection, it's bad, obviously.
One fix, if you make connections in the master process, is, instead
of closing the connection (connection.close()) invalidate it
(connection.invalidate()). Then the underlying connection is
actually closed, rather than returned to the pool.
There some on this in this thread:
https://groups.google.com/forum/#!searchin/sqlalchemy/UnicodeEncodeErrors/sqlalchemy/Xf0fWsCqdCg/5-6aRpYcuecJ
Jeff