Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Better ThreaedDict implementation using threadlocal (tx Ben Hoyt)
Using threading.local instead of managing thread-local state manually improved the performance by 50x. Also added a threadlocal implementation for python 2.3 as it doesn't have threading.local.
- Loading branch information
1 parent
895ef7f
commit 9e927c4
Showing
4 changed files
with
136 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Python 2.3 compatabilty""" | ||
import threading | ||
|
||
class threadlocal(object): | ||
"""Implementation of threading.local for python2.3. | ||
""" | ||
def __getattribute__(self, name): | ||
if name == "__dict__": | ||
return threadlocal._getd(self) | ||
elif name.startswith("__"): | ||
# for handling special atrributes like __class__ etc. | ||
return object.__getattribute__(self, name) | ||
else: | ||
try: | ||
return self.__dict__[name] | ||
except KeyError: | ||
raise AttributeError, name | ||
|
||
def __setattr__(self, name, value): | ||
self.__dict__[name] = value | ||
|
||
def __delattr__(self, name): | ||
try: | ||
del self.__dict__[name] | ||
except KeyError: | ||
raise AttributeError, name | ||
|
||
def _getd(self): | ||
t = threading.currentThread() | ||
if not hasattr(t, '_d'): | ||
# using __dict__ of thread as thread local storage | ||
t._d = {} | ||
|
||
_id = id(self) | ||
# there could be multiple instances of threadlocal. | ||
# use id(self) as key | ||
if _id not in t._d: | ||
t._d[_id] = {} | ||
return t._d[_id] | ||
|
||
if __name__ == '__main__': | ||
d = threadlocal() | ||
d.x = 1 | ||
print d.__dict__ | ||
print d.x | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9e927c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you know if this fixes/causes any memory leak ?
9e927c4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think there are any memory leaks involved in ThreadedDict implementation. Were you facing any memory leaks in your webpy app?