Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.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.007 X-Spam-Evidence: '*H*': 0.99; '*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:cs': 0.16; 'from:addr:zip.com.au': 0.16; 'from:name:cameron simpson': 0.16; 'message-id:@cskk.homeip.net': 0.16; 'opposite': 0.16; 'perceived': 0.16; 'presume': 0.16; 'received:211.29': 0.16; 'received:211.29.132': 0.16; 'received:cskk.homeip.net': 0.16; 'received:homeip.net': 0.16; 'received:optusnet.com.au': 0.16; 'received:syd.optusnet.com.au': 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; 'cheers,': 0.24; 'looks': 0.24; 'cc: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; 'but': 0.35; 'there': 0.35; 'cancel': 0.36; 'executing': 0.36; 'reality': 0.36; 'received:com.au': 0.36; 'vice': 0.36; 'doing': 0.36; 'charset:us- ascii': 0.36; 'wrong': 0.37; 'two': 0.37; 'being': 0.38; 'gmail': 0.38; 'received:211': 0.38; 'tasks': 0.38; 'sure': 0.39; 'catch': 0.60; 'signal': 0.60; 'simple,': 0.60; 'simple': 0.61; 'content- disposition:inline': 0.62; 'complete': 0.62; 'real': 0.63; 'more': 0.64; 'taking': 0.65; 'to:addr:gmail.com': 0.65; 'jobs': 0.68; 'roles': 0.68; 'started.': 0.68; 'user,': 0.69; 'safe': 0.72; 'guaranteed': 0.75; 'cancellation': 0.84; 'pursuit': 0.84; 'activity,': 0.91; 'execution,': 0.91 Date: Tue, 7 Jan 2014 13:29:58 +1100 From: Cameron Simpson To: Skip Montanaro Subject: Re: Trying to wrap my head around futures and coroutines MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) References: X-Optus-CM-Score: 0 X-Optus-CM-Analysis: v=2.1 cv=YYGEuWhf c=1 sm=1 tr=0 a=YuQlxtEQCowy2cfE5kc7TA==:117 a=YuQlxtEQCowy2cfE5kc7TA==:17 a=ZtCCktOnAAAA:8 a=PO7r1zJSAAAA:8 a=LcaDllckn3IA:10 a=LsRtkfLaIPgA:10 a=kj9zAlcOel0A:10 a=vrnE16BAAAAA:8 a=1Lml2Qfg354A:10 a=pGLkceISAAAA:8 a=sSDgrhBX7C7xfSADz34A:9 a=CjuIK1q_8ugA:10 a=MSl-tDqOz04A:10 Cc: Python 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: 66 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1389061809 news.xs4all.nl 2936 [2001:888:2000:d::a6]:43583 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63404 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() 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? Cheers, -- Cameron Simpson Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal. - Friedrich Nietzsche