I have a connection to an external resource that I need to make for my Pylons app (think along the lines of a database connection.) There is a modest amount of overhead involved in establishing the connection.
I could setup a piece of middleware that opens and closes the connection with every request but that seems wasteful. I’d like to establish a connection for each new thread that starts up and save myself the overhead. How do I hook into thread startup in Pylons?
Do the connections have to belong to a single thread for their lifetime?
If not, you could consider implementing your own connection pool for this resource. The pool would be responsible for initializing connections and each thread would
acquireandreleasethe connections as they are needed.If you want to limit the number of available connections, you just block during the acquire phase until a connection is released or some timeout is reached.
The code to implement such a pool is going to be very dependent on the resource you are talking about, so it would be difficult to give you anything other than a suggested API.