25 | | Template loader could use either a function-level lock or a module-level lock. |
| 26 | Template loader can use a function-level lock. |
| 27 | |
| 28 | '''FIXME: module and class level locks missing.''' |
| 29 | The implementation is a modified copy of http://www.phyast.pitt.edu/~micheles/python/documentation.html and should perhaps reside in `django.utils.locking`: |
| 30 | {{{ |
| 31 | from django.utils._threading_local import RLock |
| 32 | |
| 33 | def getattr_(obj, name, default_thunk): |
| 34 | "Similar to .setdefault in dictionaries." |
| 35 | try: |
| 36 | return getattr(obj, name) |
| 37 | except AttributeError: |
| 38 | default = default_thunk() |
| 39 | setattr(obj, name, default) |
| 40 | return default |
| 41 | |
| 42 | def locked(func, *args, **kw): |
| 43 | lock = getattr_(func, "__lock", RLock) |
| 44 | lock.acquire() |
| 45 | try: |
| 46 | result = func(*args, **kw) |
| 47 | finally: |
| 48 | lock.release() |
| 49 | return result |
| 50 | }}} |
| 51 | |
| 52 | == Notes == |
| 53 | |
| 54 | Once older python version support will be dropped in distant future, locking should be implemented with `with`: http://docs.python.org/lib/with-locks.html |