Changes between Version 1 and Version 2 of DjangoSpecifications/Core/Threading


Ignore:
Timestamp:
Apr 4, 2008, 5:29:15 AM (16 years ago)
Author:
mrts
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoSpecifications/Core/Threading

    v1 v2  
    2020 * module-level locks
    2121 * class-level locks
     22 * instance-level locks
    2223 * function-level locks
    2324
    2425I don't see any use for a Django-wide lock. Module-level lock is needed for sessions. Class-level locks seem appropriate for models.
    25 Template loader could use either a function-level lock or a module-level lock.
     26Template loader can use a function-level lock.
     27
     28'''FIXME: module and class level locks missing.'''
     29The implementation is a modified copy of http://www.phyast.pitt.edu/~micheles/python/documentation.html and should perhaps reside in `django.utils.locking`:
     30{{{
     31from django.utils._threading_local import RLock
     32
     33def 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
     42def 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
     54Once older python version support will be dropped in distant future, locking should be implemented with `with`: http://docs.python.org/lib/with-locks.html
Back to Top