Graham Dumpleton
grahamd at dscpl.com.au
Sun Oct 10 21:13:30 EDT 2004
On 10/10/2004, at 1:55 PM, Graham Dumpleton wrote: > The point I am trying to make is that perhaps locking shouldn't > pertain just > to the global data and that executable code should in some way be > protected as well. > It needs more exploration, but perhaps the module should read > something like the > following (untested). > > if not globals().has_key("datalock"): > # This is a brand new import and we are inside the scope of the > # import lock, so this should all be safe to execute. > > datalock = Lock() > codelock = Lock() > > myglobal1 = None > myglobal2 = None > myglobal3 = None > > ... arbitrary code to initialise globals > > codelock.acquire() > datalock.acquire() > > ... extra code to fiddle the globals > > def modify1(req): > datalock.acquire() > ... > datalock.unlock() > return value > > def modify2(req,value): > datalock.acquire() > ... > datalock.unlock() > > def _handler(req): > value = modify1(req) > modify2(req,value) > ... > > def handler(req): > # code for this method should never be changed > try: > codelock.acquire() > result = _handler(req) > finally: > codelock.unlock() > return result > > datalock.unlock() > codelock.unlock() > > What is trying to be done here is using a lock to ensure that any code > itself is > not replaced while the handler is being executed. The reason for two > level handler > arrangement, is that the lock actually needs to be acquired outside of > the real > handler method being called. If it is done inside, it is too late, as > Python > has already grabbed the existing code for handler and if it gets > replaced prior to > the lock being acquired, everything may have changed. > > Am I being too paranoid? I don't understand enough about how code gets > replaced when > a module reload is occurring. Hmmm, maybe gone slightly too far here. The code lock effectively serialises access to the handler, which screws up the whole idea of having threads in the first place. I really like this idea of using execfile() as described in other email and copying across state variables of interest into a new module, as avoids the need for the code lock anyway as new code constructed in new module. -- Graham Dumpleton (grahamd at dscpl.com.au)
|