Cross Platform Locking: THe Easy Way

Moshe Zadka moshez at math.huji.ac.il
Sun Oct 29 16:34:19 EST 2000


On Sun, 29 Oct 2000, Jerome Chan wrote:

> In article <mailman.972831527.7418.python-list at python.org>, Moshe Zadka 
> <moshez at math.huji.ac.il> wrote:
> 
> > OK, here's an easy way to do crossplafrom locking:
> > Run the following Python script, and then
> > 
> > 1) To lock, use urllib to grab the "/lock" url
> > 2) To unlick , use urllib to grab the "/unlock" url
> > 
> > You need to get this to run on system startup.
> > On UNIX, put it in rc.d.
> > On NT, do whatever it is NT guys do <wink>
> > 
> > #Released to the public domain by Moshe zadka
> > import BaseHTTPServer
> > 
> > locked = 0
> > 
> > class LockHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
> > 
> >     def do_GET(self):
> >         if self.path=="/lock":
> >             return self.lock()
> >         if self.path=="/unlock":
> >             return self.unlock()
> > 	self.send_error(404, "Only locking")
> > 
> >     def lock(self):
> >         global locked
> >         if locked:
> >             self.send_error(403, "Already locked")
> >         else:
> >             locked = 1
> >             self.send_response(202)
> >             self.end_headers()
> > 
> >     def unlock(self):
> >         global locked
> >         if not locked:
> >             self.send_error(403, "Not locked")
> >         else:
> >             locked = 0
> >             self.send_response(202)
> >             self.end_headers()
> > 
> > def test(HandlerClass = LockHTTPRequestHandler,
> >          ServerClass = BaseHTTPServer.HTTPServer):
> >     BaseHTTPServer.test(HandlerClass, ServerClass)
> > 
> > 
> > if __name__ == '__main__':
> >     test()
> 
> What if I want to lock and unlock on different files? Or different 
> sections of a file?

Use the no-granularity locking presented here in ensuring atomicity
of finer-granularity locking.
(Tiny problems: this doesn't work either in the face of crashing
applications)
--
Moshe Zadka <moshez at math.huji.ac.il> -- 95855124
http://advogato.org/person/moshez





More information about the Python-list mailing list