[mod_python] Custom handler thread safety

Huzaifa Tapal huzaifa at hostway.com
Fri Feb 4 14:56:42 EST 2005


Hello Graham,

Below are the Y/N answers to your questions:

> What you need to do will depend on a few things.
> 
> Is the Python database connection object internally thread safe?

-- not its not.

> 
> For a particular request, does it only need to use the database
> connection once, or does it need to perform multiple actions
> against the database of which all together she be performed
> atomically?

-- the way it currently works is that we have a Data Access Layer that keeps
track of all the resources any of its components needs, including database
connections to various databases as those database connections are
requested.  So to answer your question, during the life of a request, a
database connection to a particular database can be used multiple times in
different modules, and all of those modules get the handle on that
connection by asking the data access layer's resource module.  

> 
> Are the results of any database query being cached within your
> shared data area?

-- no results from the db gets cached

> 
> Do you have PythonAutoReload set to "On" and relying on that
> working for the module which holds your database connection
> and shared data.

-- PythonAutoReload is "On", however, I am not using _apache.import_module
to import the module the data access layer's resource module.

> 
> Try and give at least a Y/N answer for each.
> 
> In brief though, if the database connection object is in itself thread
> safe, and that for a request only one database query/update is being
> done and nothing is being cached resulting from that, you may well
> not have to provide your own thread locks.
> 
> If this isn't the case, you may have to lock your shared data and
> database connection with a single lock, while you possibly make
> multiple queries/updates and save the result in your shared data
> area. If you don't lock at a high enough level, classic problems of
> another threading changing the database under you, or messing with
> your shared data can happen.
> 
> I ask about the PythonAutoReload as there are lots of problems there
> that most people don't seem to understand or appreciate. Thus, if you
> are using apache.import_module() to load in your module containing
> the database connections and shared data, indicate as much and I
> can explain some of the problems there.
> 
> Graham



More information about the Mod_python mailing list