[mod_python] publisher handler questions

Jorey Bump list at joreybump.com
Mon Sep 12 20:22:53 EDT 2005


akonsu wrote:

>>An underscore will prevent direct access:
>>
>> def _foo():
>>     ...
> would not this be accessible through http://host/test.py/_foo ?

No, it's a special case. Try it.

>>>2. in my .py file (that is invoked through the publisher handler
>>>again) i define some global variables. i noticed that these variables
>>>are initialised once. is this by design or just a coincidence? can i
>>>rely on this behaviour?
>>
>>It depends. A database or other persistent storage mechanism may be more
>>reliable.
> 
> i am not talking about saving user data that should go in to sessions
> in global variables. i need a list of files in a particular directory,
> and i am trying to avoid building this list on every single http
> request. what is the right strategy for handling something like this?

If you want to keep abreast of changes, then be careful to build the 
list only when it's needed.

If you don't care that much about changes to directory contents, you can 
  import a module from your path that populates a list. Due to module 
caching, this list will only be populated when it is imported. For 
example, if a.py is in your path:

  #a.py
  import os
  ls = os.listdir('tmp/test')

and b.py is your published module:

  #b.py
  import os
  import a
  def ls():
      return [os.listdir('tmp/test'), a.ls]

Then put some files there:

  cd /tmp/test
  touch 1 2 3

and visit the URL with multiple browsers:

  http://host/somedir/b/ls

It might display this:

  [['1', '2', '3'], ['1', '2', '3']]

For kicks, visit the URL a few more times, then do this:

  cd /tmp/test
  rm 1 2 3

Revisit the URL, and you might see this:

  [[], ['1', '2', '3']]

or you might see this:

  [[], []]

The point is that a.ls will only change when the module is (re)imported, 
and this is unlikely to happen globally. Since it is sometimes cached, 
you will see a performance gain, but it's at the cost of accuracy. It's 
up to you to determine if that cost is acceptable.

You can also store this information externally and routinely update it 
(database, pickle, cronjob > file, etc.), but I'd be surprised if the 
costs associated with loading it for every request are less than simply 
using the os module.

HTH












More information about the Mod_python mailing list