Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'buttons': 0.09; 'excluding': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'gui': 0.12; "wouldn't": 0.14; 'canceled': 0.16; 'cancelled?': 0.16; 'corresponds': 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; 'opposite': 0.16; 'perceived': 0.16; 'presume': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 's/he': 0.16; 'simple.': 0.16; 'simpson': 0.16; 'task.': 0.16; 'variations': 0.16; 'ignore': 0.16; 'wrote:': 0.18; 'basically': 0.19; '(the': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'header:User- Agent:1': 0.23; 'instead.': 0.24; 'skip': 0.24; 'decide': 0.24; 'looks': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'sort': 0.25; 'task': 0.26; 'header:In-Reply-To:1': 0.27; 'code': 0.31; 'that.': 0.31; '"do': 0.31; 'complete,': 0.31; "user's": 0.31; 'probably': 0.32; 'extend': 0.32; 'stuff': 0.32; 'run': 0.32; 'running': 0.33; 'core': 0.34; "i'd": 0.34; 'something': 0.35; 'received:84': 0.35; 'but': 0.35; 'there': 0.35; 'cancel': 0.36; 'executing': 0.36; 'reality': 0.36; 'vice': 0.36; 'doing': 0.36; 'wrong': 0.37; 'two': 0.37; 'being': 0.38; 'gmail': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'catch': 0.60; 'signal': 0.60; 'simple,': 0.60; 'simple': 0.61; 'complete': 0.62; 'real': 0.63; 'more': 0.64; 'chance': 0.65; 'taking': 0.65; 'jobs': 0.68; 'roles': 0.68; 'started.': 0.68; 'user,': 0.69; 'safe': 0.72; 'guaranteed': 0.75; 'cancellation': 0.84; 'safer': 0.84; 'sfxlen:4': 0.84; 'activity,': 0.91; 'execution,': 0.91; 'pfxlen:big': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=dYa5gxne c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=5FYZ9MsUIQAA:10 a=LsRtkfLaIPgA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=1Lml2Qfg354A:10 a=pGLkceISAAAA:8 a=GGdO5pnX22TBGVkrHAYA:9 a=QEXdDO2ut3YA:10 a=MSl-tDqOz04A:10 X-AUTH: mrabarnett:2500 Date: Tue, 07 Jan 2014 03:23:32 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.2.0 MIME-Version: 1.0 To: python-list@python.org CC: python-list@python.org Subject: Re: Trying to wrap my head around futures and coroutines References: <20140107022958.GA59457@cskk.homeip.net> In-Reply-To: <20140107022958.GA59457@cskk.homeip.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit 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: 74 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389065013 news.xs4all.nl 2855 [2001:888:2000:d::a6]:35059 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63409 On 2014-01-07 02:29, Cameron Simpson wrote: > On 06Jan2014 18:56, Skip Montanaro wrote: > [...] >> Let's say I have a dead simple GUI with two buttons labeled, "Do A" and "Do >> B". Each corresponds to executing a particular activity, A or B, which take >> some non-zero amount of time to complete (as perceived by the user) or >> cancel (as perceived by the state of the running system - not safe to run A >> until B is complete/canceled, and vice versa). The user, being the fickle >> sort that he is, might change his mind while A is running, and decide to >> execute B instead. (The roles can also be reversed.) If s/he wants to run >> task A, task B must be canceled or allowed to complete before A can be >> started. > > I take it we can ignore user's hammering on buttons faster than > jobs can run or be cancelled? > >> Logically, the code looks something like (I fear Gmail is going to >> destroy my indentation): >> >> def do_A(): >> when B is complete, _do_A() >> cancel_B() > [...] >> def _do_A(): >> do the real A work here, we are guaranteed B is no longer running > [...] >> cancel_A and cancel_B might be no-ops, in which case they need to start up >> the other calculation immediately, if one is pending. > > I wouldn't have cancel_A do this, I'd have do_A do this more overtly. > >> This is pretty simple execution, and if my job was this simple, I'd >> probably just keep doing things the way I do now, which is basically to >> catch a "complete" or "canceled" signal from the A and B tasks and execute >> the opposite task if it's pending. But it's not this simple. In reality >> there are lots of, "oh, you want to do X? You need to make sure A, B, and C >> are not active." And other stuff like that. > > What's wrong with variations on: > > from threading import Lock > > lock_A = Lock() > lock_B = Lock() > > def do_A(): > with lock_B(): > with lock_A(): > _do_A() > > def do_B(): > with lock_A(): > with lock_B(): > _do_B() > It's safer to lock in the same order to reduce the chance of deadlock: def do_A(): with lock_A(): with lock_B(): _do_A() def do_B(): with lock_A(): with lock_B(): _do_B() > You can extend this with multiple locks for A,B,C provided you take > the excluding locks before taking the inner lock for the core task. > > Regarding cancellation, I presume your code polls some cancellation > flag regularly during the task? >