Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #32067
| Path | csiph.com!usenet.pasdenom.info!news.albasani.net!news.mixmin.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <ian.g.kelly@gmail.com> |
| 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; 'else:': 0.03; 'try:': 0.07; 'ugly': 0.07; 'any.': 0.09; 'loop.': 0.09; 'subject:()': 0.09; 'def': 0.10; 'thread': 0.11; '24,': 0.16; 'event.wait()': 0.16; 'fine.': 0.16; 'heapq': 0.16; 'mutex': 0.16; 'oct': 0.16; 'subject:lock': 0.16; 'threading': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'bit': 0.21; 'import': 0.21; 'header:In-Reply-To:1': 0.25; 'first,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'queue': 0.29; 'skip:q 20': 0.29; 'things,': 0.29; 'skip:_ 10': 0.29; 'probably': 0.29; 'class': 0.29; 'received:209.85.215.46': 0.30; 'skip:s 30': 0.33; 'rid': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'conditions.': 0.35; 'false': 0.35; 'pm,': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'next': 0.35; 'except': 0.36; 'but': 0.36; 'skip:p 20': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'more': 0.63; 'to:name:python': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=9uE6YXkp5O81xoARFH15d94k0kJiBdLLCpmpGTKvzfI=; b=YjJSI3tLwttAXxQcVojDhAryiEAsmMUojETCFlyqZB/A/p5J/t0usmxmfQPOjpDK2z +m0Lnkr4HzdmyXF2bu4i3/1gSTAwS6AUxaj/tdMQmRGwRPUam6rGx9/1bAGIDeuczjQb mzJ7aH9+nwfHy9jlEmwP8ZVKmUosUe3hAWeJ3uq4tDdan0OWRJZZ7c3ne/0z7x5WFIbU BWMlU907CxIaEOA+w3FDj74aEw/a8uFhXnYb5s8MnKTQ6l4nTvVnxRLVVGE7c62/dti9 2D51u3NfGENfQ/jl7qENuQYYjHLhzzNDoNuPMFs5Stn2z9Qk+TLNDBk3YG3snhZBEdkx mMbQ== |
| MIME-Version | 1.0 |
| In-Reply-To | <CALwzidm-CnE2TmizjSTU_Cx4iCgzpX14CeTu=ZotsHBX2u2DAQ@mail.gmail.com> |
| References | <OFB845143C.E36D6F51-ON85257AA1.0066C7C0-85257AA1.0067DD35@us.ibm.com> <CALwzidm-CnE2TmizjSTU_Cx4iCgzpX14CeTu=ZotsHBX2u2DAQ@mail.gmail.com> |
| From | Ian Kelly <ian.g.kelly@gmail.com> |
| Date | Wed, 24 Oct 2012 15:00:09 -0600 |
| Subject | Re: A lock that prioritizes acquire()s? |
| To | Python <python-list@python.org> |
| Content-Type | text/plain; charset=ISO-8859-1 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.2796.1351112441.27098.python-list@python.org> (permalink) |
| Lines | 44 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1351112441 news.xs4all.nl 6963 [2001:888:2000:d::a6]:37288 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:32067 |
Show key headers only | View raw
On Wed, Oct 24, 2012 at 2:19 PM, Ian Kelly <ian.g.kelly@gmail.com> wrote:
> I used a PriorityQueue and Conditions to get rid of the ugly while True loop.
Same things, but with Events instead of Conditions. This is just a
bit more readable.
The PriorityQueue is also probably unnecessary, since it's always
accessed with the mutex held. A heapq would be fine.
import threading
import Queue
class PriorityLock(object):
def __init__(self):
self._is_available = True
self._mutex = threading.Lock()
self._waiter_queue = Queue.PriorityQueue()
def acquire(self, priority=0):
self._mutex.acquire()
# First, just check the lock.
if self._is_available:
self._is_available = False
self._mutex.release()
return True
event = threading.Event()
self._waiter_queue.put((priority, event))
self._mutex.release()
event.wait()
# When the event is triggered, we have the lock.
return True
def release(self):
self._mutex.acquire()
# Notify the next thread in line, if any.
try:
_, event = self._waiter_queue.get_nowait()
except Queue.Empty:
self._is_available = True
else:
event.set()
self._mutex.release()
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: A lock that prioritizes acquire()s? Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-24 15:00 -0600
csiph-web