Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'finally:': 0.07; 'decorator': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'django': 0.11; 'def': 0.12; '**kwargs)': 0.16; '**kwargs):': 0.16; 'non-blocking': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Thread': 0.16; 'do,': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'this?': 0.23; 'header:User-Agent:1': 0.23; 'looks': 0.24; 'environment': 0.24; 'question': 0.24; "i've": 0.25; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'michael': 0.29; 'raise': 0.29; "i'm": 0.30; 'requests': 0.31; 'anyone': 0.31; 'class': 0.32; 'run': 0.32; 'subject:from': 0.34; 'something': 0.35; 'but': 0.35; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'serving': 0.60; 'skip:n 10': 0.64; 'spot': 0.65; 'tasks.': 0.68; 'url:a': 0.72; 'adapted': 0.84; 'condition.': 0.84; 'subject:being': 0.84; 'race': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Thread-safe way to prevent decorator from being nested Date: Fri, 07 Jun 2013 11:07:14 +0200 Organization: None References: <21b0b719-cdf9-4475-81ac-a429a1e65907@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849d33.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1370596054 news.xs4all.nl 15936 [2001:888:2000:d::a6]:45872 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:47321 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