Skip to content

Commit

Permalink
[tools] Some new APIs in WillieMemory
Browse files Browse the repository at this point in the history
Close issue #294

And we are ready for 4.0 release!
  • Loading branch information
Elad Alfassa committed Jun 24, 2013
1 parent a1fd333 commit 0e54954
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions willie/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,23 @@ def __setitem__(self, key, value):
self.lock.release()
return result

def contains(self, key):
def __contains__(self, key):
"""
Check if a key is in the dict. Use this instead of the ``in``
keyword if you want to be thread-safe.
Check if a key is in the dict, locking it for writes when doing so.
"""
self.lock.acquire()
result = (key in self)
result = dict.__contains__(self, key)
self.lock.release()
return result

def contains(self, key):
""" Backwards compatability with 3.x, use `in` operator instead """
return self.__contains__(key)

def lock():
""" Lock this instance from writes. Useful if you want to iterate """
return self.lock.acquire()

def unlock():
""" Release the write lock """
return self.lock.release()

0 comments on commit 0e54954

Please sign in to comment.