Distributed lock manager for Python

后端 未结 4 1795
-上瘾入骨i
-上瘾入骨i 2020-11-30 06:57

I have a bunch of servers with multiple instances accessing a resource that has a hard limit on requests per second.

I need a mechanism to lock the access on this re

4条回答
  •  被撕碎了的回忆
    2020-11-30 07:43

    Your requirements seem very specific. I'd consider writing a simple lock server then implementing the locks client side with a class that acquires a lock when it is created then deletes the lock when it goes out of scope.

    class Lock(object):
        def __init__(self,resource):
            print "Lock acquired for",resource
            # Connect to lock server and acquire resource
    
        def __del__(self):
            print "Lock released"
            # Connect to lock server and unlock resource if locked
    
    def callWithLock(resource,call,*args,**kwargs):
        lock = Lock(resource)
        return call( *args, **kwargs )
    
    def test( asdf, something="Else" ):
        return asdf + " " + something
    
    if __name__ == "__main__":
        import sys
        print "Calling test:",callWithLock( "resource.test", test, sys.argv[0] )
    

    Sample output

    $ python locktest.py 
    Calling test: Lock acquired for resource.test
    Lock released
    locktest.py Else
    

提交回复
热议问题