[mod_python] Custom handler thread safety

Graham Dumpleton grahamd at dscpl.com.au
Thu Feb 3 19:44:35 EST 2005


Huzaifa Tapal wrote ..
> Thanks Graham, your response helped a lot.  I wanted to ask another question
> related to my original post.  If I am taking advantage of the shared memory
> that using multithreaded MPM and storing objects such as db connections
> and
> parsed page templates, do you see a threat with not locking the thread
> when
> making use of those shared resources.
> 
> To be more clear, if I have resource object A in the shared memory and
> without any kind of thread locking in place, Thread 1 is using lets say
> the
> database connection object, at the same time Thread 2 needs the same
> connection object, would that cause any type of a Race Condition?
> 
> Do you think it's a good idea to wrap usage of those shared object such
> as
> when I am retrieving it from the cache with the lock.acquire() call on
> the
> thread?

What you need to do will depend on a few things.

Is the Python database connection object internally thread safe?

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?

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

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

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