views:

157

answers:

2

I'm running a Python server with mod_python, and I've run into some issues with configuration variables. This is actually two questions rolled into one, because I think they are highly related:

  1. I need a way to configure variables that will be available in Python while running. I currently just have a module that sets some name-value pairs that I import into other modules, but I was reading up on PythonOption recently and was wondering what advantages would be gained from using that instead.

  2. I need a way to store state on the server. I've got access to an API that's limited to running X number of times a day, and once it hits that limit, I need to revert to my (lesser) code. I'm wondering how I can keep track of how many times I've run the query in a day.

I thought about using a file or the database, but I'm afraid I will slow down requests by having everyone try to access the same file or row at once. Is there a better way to set this up in mod_python?

+1  A: 
  1. Using PythonOption lets you configure stuff that may need to change from server to server. I wouldn't use it too much, though, because messing with the Apache configuration directives is kind of a pain (plus it requires reloading the server). You might consider something like using PythonOption to specify the name of a settings file that contains the actual configuration variables. (Or you could just look in a standard location for the settings file, like most frameworks do)

  2. If you really don't want to consider a file or a database, try memcached. It's basically a very simple database (get, set, and clear by key only) that's stored entirely in RAM, so it's very fast. If all you need to store is a counter variable, though, you could probably just stick it in a Python module as a global variable, unless you're worried about the counter being reset when the module gets reloaded.

David Zaslavsky
A: 
  1. I need a way to configure variables that will be available in Python while running.

    Do what Django does. Use a simple importable script of Python assignment statements.

  2. I need a way to store state on the server.

    Do what Django does. Use SQLite3.

Also, read PEP 333 and structure your application components to support WSGI. This should be a relatively small revision to have the proper WSGI structure to existing code.

When you switch from mod_python to mod_wsgi, you can find numerous components to do these things for you and reduce the amount of code you've written.

S.Lott