Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Passing data across callbacks in ThreadPoolExecutor Date: Wed, 17 Feb 2016 09:24:31 -0700 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de sWdgOkr1BSyOJY+VkojbTQJFQHThlMdkHu/3Adph5nBg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; '16,': 0.03; 'received:209.85.223': 0.03; 'dependency': 0.07; 'input,': 0.09; 'threads.': 0.09; 'thread': 0.10; 'output': 0.13; 'def': 0.13; 'skip:f 30': 0.15; '2016': 0.16; 'chained': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'threads': 0.16; 'wrote:': 0.16; 'otherwise,': 0.20; '(or': 0.23; 'feb': 0.23; 'header:In-Reply- To:1': 0.24; 'separate': 0.27; 'message-id:@mail.gmail.com': 0.27; 'specify': 0.27; 'adequate': 0.29; 'task': 0.30; 'probably': 0.31; "can't": 0.32; 'facility': 0.33; 'is?': 0.33; 'tue,': 0.34; 'handle': 0.34; 'received:google.com': 0.35; 'execution': 0.35; 'tasks': 0.35; 'there': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'done.': 0.37; 'detail': 0.38; 'received:209': 0.38; 'submit': 0.39; 'skip:e 20': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'easy': 0.60; 'waiting': 0.60; 'your': 0.60; "you'll": 0.61; 'more': 0.63; 'subject:skip:T 10': 0.66; 'tasks.': 0.66; 'increase': 0.73; 'to:name:python': 0.84; 'sitting': 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=koctiQkAV/CRIJn+CZ7RCYR1VHNlt/WLZ5lVbZ1YZKk=; b=cQG4ORhrnSsERkQNymlVrGJcpgGB6Bn4xqm4cRYdT/p+sEqtk3lyFDERWygHIFyaTe 3eovH+76gldRNpnU/uQutJi8LKcMf2twMNrvA8tAF0CqFKpwZWsSOvjhUKnXZ7qIMwBi 9qHHk1UCs5f5TNTVtGTdEnvdJLd+6oIg0MvIv7Yi5aSvMbxf5LssRUv4+PnvibktHvde CUotzMAEBWlNw5YX9MW+OvAzaahnO6SH0aHBybt9aVvJrSAAvFSfL/Mk/zny/6suD0G2 3BSo8XFopXDKM5NtIviSKqVumeYl8O9qeYkGfE4583leImUNBL7qHEOxZmpohXS6uTuN sz3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:content-type; bh=koctiQkAV/CRIJn+CZ7RCYR1VHNlt/WLZ5lVbZ1YZKk=; b=KeNEPn0iF0/jtIgOLrpN90xEXmmx7WT2e7g8ngFUxE1hHbxb8bEa2o9zHErHYPcKwT juOcopr4Irg0Dz/3659x0lhGQo7Dl7KJABXmdHiNvFIhtcxTmov98L4AXIAqYEJQJ6iq Qw0lVNWxe+uCasjo3ZOL1QVpPPr3DorXD710hpXbFeibb1VLEZA0eETsMSWRRGIovNtY U1rMWc5txm8Ey4YAf7GPJVPikh/Efv9FwVqbgG7UAnAaAtCogg4sekAK8lW14J0hH2Xo nsUS15mLwR4nM6d5Xx2xqFezASWX4vJCjTwy3zid2ckQ5xf7eTvMU+XCiOND+fd25KpM Y0PA== X-Gm-Message-State: AG10YOQXRzkA5viEGopASlPcJhFSdvFVFMK0kdwDhdHfFP1uqMNDn1SDB5zmC9SGo/90tonCeJ0eOhGoq5Orbg== X-Received: by 10.107.11.93 with SMTP id v90mr4139397ioi.188.1455726310684; Wed, 17 Feb 2016 08:25:10 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:103056 On Tue, Feb 16, 2016 at 2:48 PM, Joseph L. Casale wrote: > What is the pattern for chaining execution of tasks with ThreadPoolExecutor? > Callbacks is not an adequate facility as each task I have will generate new output. Can you specify in more detail what your use case is? If you don't mind having threads sitting around waiting, you can just submit each chained task at the start and have each task wait on the futures of its dependencies. The advantage of this is that it's easy to conceptualize the dependency graph of the tasks. The disadvantage is that it eats up extra threads. You'll probably want to increase the size of the thread pool to handle the waiting tasks (or use a separate ThreadPoolExecutor for each chained task). E.g.: def task2(input, f1): f1_result = f1.result() # Wait for f1 to be done. result = frobnicate(input, f1_result) return result f1 = executor.submit(task1, input) f2 = executor.submit(task2, input, f1) f1.add_done_callback(handle_task1_done) f2.add_done_callback(handle_task2_done) Otherwise, is there some reason you can't use multiple callbacks, one to handle the task's output and one to submit the chained task? E.g.: def chain_task2(input, f2): f2 = executor.submit(task2, input, f2.result()) f2.add_done_callback(handle_task2_done) f1 = executor.submit(task1, input) f1.add_done_callback(handle_task1_done) f1.add_done_callback(functools.partial(chain_task2, input))