Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'acquired': 0.04; 'finally:': 0.05; 'try:': 0.07; 'anymore.': 0.09; 'correct,': 0.09; 'mess': 0.09; 'subject:()': 0.09; 'thread,': 0.09; 'underlying': 0.09; 'def': 0.10; 'thread': 0.11; 'anymore': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'occurs,': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'request)': 0.16; 'semantically': 0.16; 'silly': 0.16; 'subject:lock': 0.16; 'threads': 0.16; 'true:': 0.16; 'zero,': 0.16; 'wrote:': 0.17; 'basically': 0.17; 'tries': 0.17; 'requests': 0.18; 'question.': 0.20; 'bit': 0.21; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'looks': 0.26; 'possible,': 0.27; 'thoughts': 0.27; "doesn't": 0.28; 'run': 0.28; 'finds': 0.29; 'priority': 0.29; 'queue': 0.29; 'received:192.168.1.3': 0.29; 'second,': 0.29; 'starts': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; 'seconds': 0.30; 'up.': 0.31; 'code': 0.31; 'gets': 0.32; 'received:84': 0.32; 'running': 0.32; 'skip:s 30': 0.33; 'like:': 0.33; 'point,': 0.33; 'retain': 0.33; 'skip:l 40': 0.33; 'to:addr:python-list': 0.33; 'requirements': 0.33; 'subject:?': 0.35; 'something': 0.35; 'there': 0.35; 'next': 0.35; 'add': 0.36; 'but': 0.36; 'method': 0.36; 'anything': 0.36; 'too': 0.36; 'skip:p 20': 0.36; 'one,': 0.37; 'two': 0.37; 'ones': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'release': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'think': 0.40; 'most': 0.61; 'first': 0.61; 'maximum': 0.63; 'times': 0.63; 'more': 0.63; 'header:Reply- To:1': 0.68; 'reply-to:no real name:2**0': 0.72; 'discover': 0.72; 'as:': 0.75; 'hand': 0.82; 'importance.': 0.84; 'reply- to:addr:python.org': 0.84; 'start.': 0.84; 'thing).': 0.84; 'average': 0.93 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=YaM/Fntf c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=b2nRVtXOy8EA:10 a=anh_DZ2Q55AA:10 a=ihvODaAuJD4A:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=EBOSESyhAAAA:8 a=8AHkEIZyAAAA:8 a=UCB52jM2zRkA:10 a=JDr4qq90IvwVPtSCd4QA:9 a=wPNLvfGTeEIA:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Wed, 24 Oct 2012 20:41:37 +0100 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:16.0) Gecko/20121010 Thunderbird/16.0.1 MIME-Version: 1.0 To: python-list@python.org Subject: Re: A lock that prioritizes acquire()s? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list Reply-To: python-list@python.org 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: 109 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351107697 news.xs4all.nl 6891 [2001:888:2000:d::a6]:44088 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32063 On 2012-10-24 19:54, David M Chess wrote: > > Okay, next silly question. :) > > We have a very simple multi-threaded system where a request comes in, > starts running in a thread, and then (zero, one, or two times per > request) gets to a serialization point, where the code does: > > with lock: > do_critical_section_stuff_that_might_take_awhile() > > and then continues. > > Which is almost the same as: > > lock.acquire() > try: > do_critical_section_stuff_that_might_take_awhile() > finally: > lock.release() > > Now we discover that It Would Be Nice if some requests got priority over > others, as in something like: > > lock.acquire(importance=request.importance) > try: > do_critical_section_stuff_that_might_take_awhile() > finally: > lock.release() > > and when lock.release() occurs, the next thread that gets to run is one > of the most important ones currently waiting in acquire() (that's the > exciting new thing). > > Other requirements are that the code to do this be as simple as > possible, and that it not mess anything else up. :) > > My first thought was something like a new lock-ish class that would do > roughly: > > class PriorityLock(object): > > def __init__(self): > self._lock = threading.Lock() > self._waiter_map = {} # maps TIDs to importance > > def acquire(self,importance=0): > this_thread = threading.currentThread() > self._waiter_map[this_thread] = importance # I want in > while True: > self._lock.acquire() > if ( max( self._waiter_map.values())<=importance ): # we win > del self._waiter_map[this_thread] # not waiting anymore > return # return with lock acquired > self._lock.release() # We are not most impt: release/retry > > def release(self): > self._lock.release() > > (Hope the mail doesn't garble that too badly.) > > Basically the acquire() method just immediately releases and tries again > if it finds that someone more important is waiting. > > I think this is semantically correct, as long as the underlying lock > implementation doesn't have starvation issues, and it's nice and simple, > but on the other hand it looks eyerollingly inefficient. > > Seeking any thoughts on other/better ways to do this, or whether the > inefficiency will be too eyerolling if we get say one request per second > with an average service time a bit under a second but maximum service > time well over a second, and most of them are importance zero, but every > (many) seconds there will be one or two with higher importance. > Here's my take on it: class PriorityLock(object): def __init__(self): self._lock = threading.Lock() self._waiter_queue = [] self._queue_lock = threading.Lock() def acquire(self, importance=0): this_thread = threading.currentThread() # Add this thread to the queue with self._queue_lock: self._waiter_queue.append((importance, this_thread)) self._waiter_queue.sort(reverse=True, key=lambda pair: pair[0]) # Move the most important to the start. # Acquire and retain the lock when this thread is at the start of the queue. while True: self._lock.acquire() with self._queue_lock: if self._waiter_queue[0][1] == this_thread: # We win. del self._waiter_queue[0] # Not waiting anymore. return # Return with lock acquired. self._lock.release() # We are not most important: release and retry. time.sleep(0.01) # Give the other threads a chance. def release(self): self._lock.release()