views:

245

answers:

2

After fighting with different things here and there, I was finally able to get BottlePY running on Apache and run a MongoDB powered site. I am used to running Django apps, so I will be relating to that a bit in my question.

The Problem

Every time a page is loaded via BottlePY, the connection to the MongoDB database located on MongoHQ.com needs to be re-authenticated (meaning it probably had to reconnect).

What I Found

I attached a db.keep_alive() function to the top of each model function, so that before any mongodb query is run, it trys to run a simple query. If it fails, it catches the OperationFailure or AutoReconnect errors and then calls the db.authenticate() function. After it reauthenticates, I have it add a log to a logs db to monitor how often it needs to reauthenticate. Currently, it needs to reauthenticate on every page load (that requires running a query). This isn't right.

Difference from Django

I use this same concept in django, and have found that the db connection only needs to be authenticated after 10-15 minutes of no queries being run.

I don't understand why creating a pymongo connection in django would be different from creating one in bottle, since I am using the same driver, functions and methods. I am not using any ORMS or anything like that either.

Versions

  • Bottle: 0.9.dev
  • Django: 1.2.1 final
  • PyMongo: 1.8

I appreciate the help!

Update: A friend was able to take a quick look and noticed the following that may help with answering my question.

It appears that each request is launching a new Python process, as opposed to Django, in which a single process remains running for a long period of time.

A: 

does your apache xxx.conf contain something like:

WSGIDaemonProcess project user=mysite group=www-data processes=5 threads=1 WSGIProcessGroup project

I think most important should be threads=1

ralf.w.
It was set to 10 but I changed it to 1 and it hasn't solved the problem. Good point though, thanks!
Shane Reustle
Also try setting both processes=1 and threads=1 and see if the problem goes away. Please note that this should be only a temporary setting for debugging, because it limits the number of concurrent connections your web application can handle.
pts
Both variables were already set to 1.
Shane Reustle
A: 

This just ended up to be a weird thing between Bottle and MongoHQ. No real solution was found, but I couldn't recreate it with other frameworks. Any other ideas are appreciated.

Shane Reustle