Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: How asyncio works? and event loop vs exceptions Date: Sat, 23 Jul 2016 08:06:31 -0600 Lines: 46 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de Bkz5zb2CN7abIQOdfHXRXgziAK4huR5SgnXI5rBLwx8A== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.012 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'handler': 0.04; 'subject:How': 0.09; '22,': 0.09; 'skip:g 60': 0.09; 'exception': 0.13; 'def': 0.13; '2016': 0.16; 'async': 0.16; 'handling.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'statement.': 0.16; 'wrote:': 0.16; 'skip:g 40': 0.18; 'skip:l 30': 0.18; 'try:': 0.18; 'exceptions': 0.22; 'passing': 0.23; 'this:': 0.23; 'header :In-Reply-To:1': 0.24; 'error': 0.27; 'fri,': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'yield': 0.27; 'function': 0.28; 'task': 0.30; 'another': 0.32; "can't": 0.32; 'class': 0.33; 'except': 0.34; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'execution': 0.35; 'tasks': 0.35; 'but': 0.36; 'instead': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'to:addr:python.org': 0.40; 'further': 0.62; 'skip:n 10': 0.62; 'python-list': 0.66; 'contrary': 0.72; 'jul': 0.72; 'await': 0.76; 'returns.': 0.84; 'to:name:python': 0.84; 'task,': 0.91 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; bh=r9rbDQ5mZKXNA2ozS/osUr2Af63sP6i+aMBdzXpgjCk=; b=B5haU36Sw/Q+HWXPnhZrnFPXI0jvAAGLlyFi2gqh8wS+MB5B3m7K8gSVin1qilh1GV VlMtxDQ7wHc5g5KzI3xVFQTnNGlpsw0PWOO3rh/vEIXex6ZzSNyO8eAQL0sTzHvn9LRS nVvrPBufF3stADf+m2U7FZ9ogrhVX81jhAjP1/LsKIiLj5d2lQ99jiO03lrwIBdRAlfP 9qTdQInVpT1HxY7O6HeLvML9q7N4FFovF5HXXi9reiNGoRM/QYsGPz5QTpPdDB0B5KsI RW324MaXp0w/gQR6l+BMJ1ecXrwuo4mKt/gag62DNp+7mRTayPxoYxaVl+J3JQ4O2aG/ eKPA== 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; bh=r9rbDQ5mZKXNA2ozS/osUr2Af63sP6i+aMBdzXpgjCk=; b=NY+k0fZoyNzLltqqxWuCUvVLvHWV4dJIEorWd7L8iO6mq0Vr0R3IE15M9ghfaqVLjw 8/lYS7n5vi302vTiSXRlaXZuxSoLc8oQ8hqUcifEF7xZLgXvmFt6AHjR54yrZtoW6L7t X0MUz318BHyYpGbU4beS9rD5EFCrYcgX75vc7l2gARSmq75khUKyJfglNStL/rkCtbAE CUmG6rYnxiYeUZqKDElfOu7bEBf4DeQmnB0GKQVNaEe9SlbS3p9NiUHBOxUWJ8tsyT+d HtAzjQbowYIsH104Y/vJWQsIhokGCx8UxqeplBcm9BZzWyyzCmTsQCkQXhT58zr9qjKF r/Vw== X-Gm-Message-State: AEkoout3raI+Z+Fr2OXKk32Qwkqe/sKPH6wd3sPTBbc5LzU52rhwInniU/XzjAcLO3twa9td3NLfxKL4OKk2aw== X-Received: by 10.202.102.214 with SMTP id m83mr4292181oik.24.1469282831215; Sat, 23 Jul 2016 07:07:11 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Mailman-Original-Message-ID: X-Mailman-Original-References: Xref: csiph.com comp.lang.python:111791 On Fri, Jul 22, 2016 at 6:27 PM, Marco S. via Python-list wrote: > Furthermore I have a question about exceptions in asyncio. If I > understand well how it works, tasks exceptions can be caught only if > you wait for task completion, with yield from, await or > loop.run_until_complete(future). But in this case the coroutine blocks > the execution of the program until it returns. On the contrary you can > execute the coroutine inside an asyncio task and it will be > non-blocking, but in this case exceptions can't be caught in a try > statement. If you don't want to block the current function on the task, then spin off another task to do the error handling. Instead of this: async def do_something(): try: await do_something_else() except DidNothingError as e: handle_error(e) ... Consider this: async def do_something(): get_event_loop().create_task(await_and_handle(do_something_else())) ... async def await_and_handle(awaitable): try: await awaitable except DidNothingError as e: handle_error(e) If you want, you could generalize that further by passing in the exception class and error handler as well: async def do_something(): get_event_loop().create_task(await_and_handle( do_something_else(), DidNothingError, handle_error)) ... async def await_and_handle(awaitable, error_class, handler): try: await awaitable except error_class as e: handler(e)