Ben Bangert wrote: > On Sep 7, 2006, at 3:42 PM, Graham Stratton wrote: > > > When I ran the app using paster, I was able to maintain one connection > > per thread, which amounted to about 11 connections to the external > > system. > > Connection using what db system? If the db system you're using is > told to use a thread pool, it'll likely be the one doing so, not Pylons.
I'm using Pylons with Piers Harding's saprfc library to connect to an SAP system. Rails is the current fashionable choice, but since it gets upset when you take its database away, I thought Pylons would be a much better option. > > When I switched to mod_python, I quickly got nearly 200 connections > > (at > > which point the system refused to give any more), having made slightly > > fewer than that many requests. I then assumed that Apache would only > > allocate one request to any process at once, so I then changed my code > > to only make one connection per process. This seemed to work, and > > kept > > the number of connections at 15. (My tests were with 10 concurrent > > requests; Apache is configured to keep 5 processes free). > > > > This all suggests to me that Apache allows one request per process, > > but > > the Pylons processes spread those requests (unnecessarily) between > > threads. Is that true? Should one or the other be configured > > differently? > > In the mod_python script, it uses the wsgi setting from Apache 2.0. > So it knows to do threading if Apache is using the mpm worker, vs the > prefork worker. > I'd suggest putting a toggle in your db connection code to make it > aware of whether its in threads or not (Pylons handles a single > request at a time and has no threading of its own). The variable > you'll want to check is environ['wsgi.multithread'] and environ > ['wsgi.multiprocess']. Let me know if those are being set improperly > under mod_python. Okay, I could certainly do this. But I'd feel much happier if I understood why my current solution fails. I've looked a bit more carefully at what's going on. I have two ways of connecting: one piece of code establishes one connection per process, and the other piece of code uses a threading.local object to establish one connection per thread. When I use paster to run the app in single connection mode, I get errors, as I expected. When I use paster with the multithreaded code, I get one connection per thread, as expected. When I run it using apache with the single connection code, I get one connection per process, and all is well. But, when I use apache with the multi-threaded code, I get one connection for every single request, despite there being only a single thread per process. One last-minute bit of research turned up this thread: http://www.modpython.org/pipermail/mod_python/2006-August/021857.html It seems that something funny happens with mod_python and the C version of threading.local. I changed my code to import local from _threading_local, and now it works as expected. I'm not sure whether this the best solution, or whether I should use wsgi.multithread to decide which code to use as well or even instead. Thanks for your support, Graham --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "pylons-discuss" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/pylons-discuss -~----------~----~----~----~------~----~------~--~---
