[mod_python] Co-ordinating ccess to a single-use resource

Nicolas Lehuen nicolas.lehuen at gmail.com
Thu Sep 8 13:45:20 EDT 2005


2005/9/8, Graham Dumpleton <grahamd at dscpl.com.au>:
> 
> On 09/09/2005, at 2:23 AM, John Simeone wrote:
> 
> > Hello to everyone.
> >
> > I am responsible for a project using Apache 2.0 with mod_python in a
> > Win2k environment, the choice of OS being dictated by the fact that
> > the purpose of the server is to supply access to a proprietary,
> > supplier written Windows program to our user base via the web.
> >
> > That program is single threaded and provides for only 1 user at a
> > time. Access to the program in Python is through  win32com.
> >
> > I am using a mod_python.publisher handler.
> >
> > All the http requests come through a single proxy server with a static
> > address but not all are from browser clients. Some are originating
> > from perl programs but all requests are directed to a single request
> > handler.
> >
> > Can anyone suggest a method to control access to this single user
> > resource. In a non-web programming environment, I'd just use a mutex
> > semaphore to co-ordinate access among the threads but I am too much of
> > newbie with mod_python and Apache to implement a workable solution so
> > far.
> >
> > What mechanism can I use to create a shared resource that I can lock
> > and have other requests wait until the resource is unlocked?
> 
> When using mod_python under Win32 platform, there is effectively only
> one process and requests are handled in separate threads. Because of
> this, you should be able to mediate access to the shared resource
> through a normal thread mutual exclusion locking mechanism just as
> easily as you would in a non web environment.
> 
> Graham

That is to say :

import threading

# "R" stand for Re-entrant, i.e. a thread can acquire the lock multiple times
# make sure that the lock is instantiated only once, for example by
putting it in
# a module which your published code imports.
lock = threading.RLock() 

lock.acquire()
try:
    # access the shared resource
finally:
    lock.release()

See the documentation of the threading module for more information.

Regards,
Nicolas



More information about the Mod_python mailing list