Thread-safe way to prevent decorator from being nested
Peter Otten
__peter__ at web.de
Fri Jun 7 05:07:14 EDT 2013
More information about the Python-list mailing list
Fri Jun 7 05:07:14 EDT 2013
- Previous message (by thread): Thread-safe way to prevent decorator from being nested
- Next message (by thread): [JOB] Look for a Full Time Plone Developer
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Michael wrote: > I'm writing a decorator that I never want to be nested. Following from the > answer on my StackOverflow question > (http://stackoverflow.com/a/16905779/106244), I've adapted it to the > following. > > Can anyone spot any issues with this? It'll be run in a multi-threaded > environment serving Django requests and also be a part of Celery tasks. I'm not sure I understand what you are trying to do, but this > if not within_special_wrapper(): > with flag(): looks suspiciously like race condition. > thread_safe_globals = threading.local() I'm not an expert in the area, but I think you need a lock, something like class NestingError(Exception): pass nest_lock = threading.Lock() def my_special_wrapper(f): @wraps(f) def internal(*args, **kwargs): if nest_lock.acquire(False): # non-blocking try: f(*args, **kwargs) finally: nest_lock.release() else: raise NestingError return internal
- Previous message (by thread): Thread-safe way to prevent decorator from being nested
- Next message (by thread): [JOB] Look for a Full Time Plone Developer
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list