Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.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.082 X-Spam-Evidence: '*H*': 0.84; '*S*': 0.00; 'subject:same': 0.07; 'callback': 0.09; 'section,': 0.09; 'def': 0.12; 'enough.': 0.16; 'for,': 0.16; 'subject: \n ': 0.16; 'subject:run': 0.16; 'wrote:': 0.18; 'possible,': 0.19; 'resolved': 0.19; 'skip:f 30': 0.19; 'stack': 0.19; 'thu,': 0.19; 'examples': 0.20; '>>>': 0.22; 'example': 0.22; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; '25,': 0.31; 'argue': 0.31; 'sep': 0.31; 'probably': 0.32; 'critical': 0.32; 'subject:the': 0.34; 'skip:s 30': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:f 40': 0.36; 'transition': 0.36; 'subject:?': 0.36; 'easily': 0.37; 'to:addr :python-list': 0.38; 'moving': 0.39; 'to:addr:python.org': 0.39; 'easy': 0.60; 'above,': 0.60; 'course.': 0.60; 'ian': 0.60; 'simply': 0.61; 'simple': 0.61; 'further': 0.61; 'complete': 0.62; 'real': 0.63; 'such': 0.63; 'more': 0.64; 'subject:skip:T 10': 0.68; 'account.': 0.80; 'isolate': 0.84; 'ugly,': 0.84; 'imagine': 0.93 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=144quB817ZavQz+iH6yXVNYJ5E/6YRakIdmf5nAX+Rw=; b=lPlN+aLV/wAu34rvU1xvmGFdEz4gXWHZbdn7E9ikU+K+otiHBZrD6EnFY01cm7SjAA NfXx5nKlxBe/KtqMVcCQ4hcZJZHv9yEWwxY6PHGY3BYPtCsJEBHHPBCcfJGO932LLLJU buSC52VuZ+wYrKl7Pmm2Xi5ExQR2OczIv2Vqt7hiIsD8rxwL/85u8+uYSdchTF95Z9Km LR1/0Pi20TSNzRDziKppHrhgrlcOhXGxYMwaRBRh/aE6sgXuPcwgkQK2SgP5itTefPI4 IWhKrG0AQENQSSO2JTZBf+sYXY3y3Xe2paVRNU3kcUBysj5i8cnUcjsIwz2OzrIxFgSk 7Bzg== X-Received: by 10.66.161.41 with SMTP id xp9mr20949475pab.120.1411659674858; Thu, 25 Sep 2014 08:41:14 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <87sijf69k5.fsf@elektro.pacujo.net> References: <871tr06nhs.fsf@elektro.pacujo.net> <87sijf69k5.fsf@elektro.pacujo.net> From: Ian Kelly Date: Thu, 25 Sep 2014 09:40:34 -0600 Subject: Re: ThreadPoolExecutor - callback guaranteed to run in same thread as the submitted function? To: Python Content-Type: text/plain; charset=UTF-8 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: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1411659684 news.xs4all.nl 2845 [2001:888:2000:d::a6]:45792 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:78345 On Thu, Sep 25, 2014 at 8:47 AM, Marko Rauhamaa wrote: > Ian Kelly : > >> On Thu, Sep 25, 2014 at 3:46 AM, Marko Rauhamaa wrote: >>> Example (pseudocode): >>> >>> def callback(self): >>> with self.lock: >>> ... >>> >>> def xyz(self, f): >>> with self.lock: >>> ... >>> f.add_done_callback(self.callback) >>> >>> The code will deadlock if the callback is invoked immediately. >> >> Easily solved using a re-entrant lock. > > Not easily. Your state transition might not be complete at the time of > the preemption. Many such issues can be resolved by moving the > preemptive function calls to the end of the critical section, but that > is not always possible, easy or taken into account. Fair enough. In the simple example above, I see no harm in simply moving the add_done_callback call outside (after) the "with self.lock" block altogether. I can imagine more complex examples where the call is further down the stack and harder to isolate from the lock, although I would argue that's probably a code smell. > If preemption needs to be prepared for, the real solution is: > > > def xyz(self, f): > with self.lock: > ... > def add_callback(): > f.add_done_callback(self.callback) > self.executor.schedule(add_callback) > > which is ugly, of course. YMMV, but I think I would prefer to make the executor.schedule call be the callback: def callback(self, f): with self.lock: ... def xyz(self, f): with self.lock: ... f.add_done_callback( functools.partial(self.executor.schedule, self.callback))