Locking around

Nikolaus Rath Nikolaus at rath.org
Wed Aug 6 06:34:32 EDT 2008


Nikolaus Rath <Nikolaus at rath.org> writes:
>> This should work, at least the idea is not flawed. However, I'd say
>> there are too many locks involved. Rather, you just need a simple
>> flag and the global lock. Further, you need a condition/event that
>> tells waiting threads that you released some of the files so that it
>> should see again if the ones it wants are available.
>
> I have to agree that this sounds like an easier implementation. I
> just have to think about how to do the signalling. Thanks a lot!

Here's the code I use now. I think it's also significantly easier to
understand (cv is a threading.Condition() object and cv.locked_keys a
set()).

    def lock_s3key(s3key):
        cv = self.s3_lock

        try:
            # Lock set of locked s3 keys (global lock)
            cv.acquire()

            # Wait for given s3 key becoming unused
            while s3key in cv.locked_keys:
                cv.wait()

            # Mark it as used (local lock)
            cv.locked_keys.add(s3key)
        finally:
            # Release global lock
            cv.release()


    def unlock_s3key(s3key):
        cv = self.s3_lock

        try:
            # Lock set of locked s3 keys (global lock)
            cv.acquire()

            # Mark key as free (release local lock)
            cv.locked_keys.remove(s3key)

            # Notify other threads
            cv.notify()

        finally:
            # Release global lock
            cv.release()


Best,
            
   -Nikolaus

-- 
 »It is not worth an intelligent man's time to be in the majority.
  By definition, there are already enough people to do that.«
                                                         -J.H. Hardy

  PGP fingerprint: 5B93 61F8 4EA2 E279 ABF6  02CF A9AD B7F8 AE4E 425C



More information about the Python-list mailing list