Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Zachary Ware Newsgroups: comp.lang.python Subject: Re: Adding run_in_executor task to already existing loop. Date: Fri, 25 Mar 2016 16:00:16 -0500 Lines: 44 Sender: zachary.ware@gmail.com Message-ID: References: Reply-To: python-list@python.org Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de Sbmx9IfmiTQwS4N8UABaxAD6Rv+tWM/IjNH0Nm5lbJVw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'error:': 0.05; 'blocked': 0.09; 'i\xe2\x80\x99ve': 0.09; 'loop.': 0.09; 'received:209.85.218': 0.10; 'def': 0.13; 'producing': 0.15; '2016': 0.16; 'async': 0.16; 'elsewhere,': 0.16; 'hello:': 0.16; 'i\xe2\x80\x99m': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:Adding': 0.16; 'subject:already': 0.16; 'wrote:': 0.16; 'sender:addr:gmail.com': 0.18; 'to:name:python- list@python.org': 0.20; 'assuming': 0.22; 'trying': 0.22; 'skip:l 40': 0.23; 'this:': 0.23; 'examples': 0.24; 'header:In-Reply- To:1': 0.24; 'example': 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; 'blocking': 0.29; 'ray': 0.29; 'code:': 0.29; "i'm": 0.30; 'code': 0.30; 'task': 0.30; 'says': 0.32; 'run': 0.33; 'running': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'clear': 0.35; 'but': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'doing': 0.38; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'future': 0.60; 'your': 0.60; 'here.': 0.62; 'within': 0.64; 'mar': 0.65; 'results': 0.66; 'header:Reply- To:1': 0.67; 'reply-to:no real name:2**0': 0.71; 'await': 0.76; 'reply-to:addr:python.org': 0.84; 'we\xe2\x80\x99re': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:reply-to:sender:in-reply-to:references:from:date :message-id:subject:to:content-transfer-encoding; bh=mWI27vFG9e8BLvU1oRgwEisbV3qJN/NBfNt+uLczELc=; b=bEwKAZraif/NTRWXrGq/1oBMqfeMmu2mgvJ8WTY8UK+ptDsPWXmTtuvD4eC6rkUdB7 EbhkgiC164e99mPuPbkOmqwN9zxpWtDLjZxM1WD/3bxQtJiklUGma3z+kHa+B527rlUh KV3iHJChIyGmrS4T4uaFWz+ijHqjxJ9PZXpksd6b+o8v8HSSRr8OgI7mUWxGOt24dUPo y7aUBEVPP7h404AMy6oM/YcbuVH/bzohEO1fLzQnAGfp44+ATPVlL9DATf+Plph7faxN BKYGD1O1u2/VjR3rQOPjlG+dptqjF71FWlxURrSxdd/v2zLQPJ01Y2mlWEznWhZNsNgZ 9ZRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:reply-to:sender:in-reply-to :references:from:date:message-id:subject:to :content-transfer-encoding; bh=mWI27vFG9e8BLvU1oRgwEisbV3qJN/NBfNt+uLczELc=; b=OXetxzh3ou6VTu8p+MJ755J2o4FEqdFywDaJA0tPOXmnnlSCU0JOJjqWuqT1hJRSR1 D70+7KLR5ylMtNWto00L0VoHT7Tc3uyDNeWcQ5l+lK2Ql5h0LilDcC/WKzfRsvOS99Le TQJhxbQM8LbVcrsnlcsqpDvzsgz61D6lZ2daZyhYZqsMWLsuBRbLX52W9Mz1jAAZWccC 9voyY338FVQ5p4uvlOUOuRydjtZfEFgdsaOSJIoPIBaxk8K1ABlhp5xstbXDFgxU+olf 7uKOePA24qi43QBcWqKIZ10uwpgqu/0BbehISabN9oa+OegKcCzdeU12GpM+okOvXK3M 7rdw== X-Gm-Message-State: AD7BkJJoBipLH1Y2fRut+ML61zs4zzKjGNB4v27mFP8vBBwo9WWHLhzSPfkZ+cZRhVAdfw+noyw6WX4dJRAC/w== X-Received: by 10.202.61.85 with SMTP id k82mr7356294oia.114.1458939635899; Fri, 25 Mar 2016 14:00:35 -0700 (PDT) In-Reply-To: X-Google-Sender-Auth: Npzq8RTHO3YKBfLPls2Ny70iE6I X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:105702 On Fri, Mar 25, 2016 at 3:24 PM, Ray Cote wrote: > Hello: > > I=E2=80=99m trying to perform an synchronous task while using asyncio. > I understand the solution is to use run_in_executor. > I=E2=80=99m not clear on how to add this into an already running event lo= op. > > I=E2=80=99ve found lots of examples showing how to set up a loop and run = this, but > I=E2=80=99m blocked in regards to doing this when the loop is already est= ablished. > > > Example code: > > def blocking_func(param1): > # call the blocking call here. > return results > > async def process_request(): > loop =3D asyncio.get_event_loop() > block =3D loop.run_in_executor(None, blocking_func, =E2=80=9Chello=E2= =80=9D) > results =3D await loop.run_until_complete(asyncio.gather(*[block, ]) > > The above code says =E2=80=9Cloop already running.=E2=80=9D because we=E2= =80=99re already in an > async ask that has been awaited. What is the proper method of adding in > this new synchronous task? I'm assuming you're doing `await process_request()` elsewhere, which is what's producing your error: you're trying to start the loop within a coroutine running on that loop. loop.run_in_executor() returns a Future just like any other coroutine, so process_request just needs this: async def process_request(): loop =3D asyncio.get_event_loop() results =3D await loop.run_in_executor(None, blocking_func, 'hello') --=20 Zach