Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: How the heck does async/await work in Python 3.5 Date: Tue, 23 Feb 2016 10:37:31 -0700 Lines: 38 Message-ID: References: <56c7d145$0$1597$c3e8da3$5496439d@news.astraweb.com> <56CB88D5.3090202@mail.de> <56CC8DBD.9080604@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de iyC3n15TQyn4m9LFa4XhtwGKhbfr/jejk7qOv3sqUjUA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'compiler': 0.05; 'none:': 0.05; 'desired.': 0.07; 'see.': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; '22,': 0.09; 'python': 0.10; 'def': 0.13; 'skip:p 40': 0.15; '12:50': 0.16; '2016': 0.16; '23,': 0.16; 'async': 0.16; 'c++)': 0.16; 'cc:name:python': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'thanks.': 0.18; '>>>': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'explicit': 0.22; 'implicit': 0.22; 'am,': 0.23; 'seems': 0.23; 'feb': 0.23; 'header:In-Reply- To:1': 0.24; 'mon,': 0.24; 'message-id:@mail.gmail.com': 0.27; 'loop,': 0.29; 'received:209.85.213.174': 0.29; 'subject:/': 0.30; 'point': 0.33; 'call,': 0.33; 'wrap': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'done': 0.35; 'clear': 0.35; 'c++': 0.35; 'functions.': 0.35; 'something': 0.35; 'level': 0.35; "isn't": 0.35; 'but': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'subject:work': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'doing': 0.38; 'received:209': 0.38; 'drop': 0.38; 'anything': 0.38; 'skip:p 20': 0.38; 'subject:the': 0.39; 'easily': 0.39; 'your': 0.60; 'is.': 0.63; 'more': 0.63; 'different': 0.63; 'hand': 0.82; 'awaits': 0.84; 'round.': 0.84; 'to:none': 0.91; 'technically': 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:cc; bh=PHz/GkGaP/x6yShTVClmF9uuG2KG9hCKZYwoVc9O428=; b=PUWd7ZP+a9eAjvZ9aIuk9x81mWDtuzkNBHxEQ5L+roT4cEbdJOc7nthjSRx88XSS1Z 5fRiu/6PlpYtL1zlbwJ8/WmIfFIeNqZ2QYeqwLTM0xJAA3V2yjyZE5l+M9nb1Tf+n9k4 paENjPiv+LNkZft3ExYgegDmMzc6mHSY/arAWObOgsLLYH0CGtVvRPk2DmbPmr9ylgJd p1IgvdlC05j80Fo+EXSC0h36uR5G7WyixbFJT4wo6Nsdq5wld9XA61ycYpO5uY6IrE6p Gn+39B6JPLtSRjciP2jC0h28Ml37tJNgNg6rMZz2BxfKr6FLyufLnRppgc4YNuGjM5W7 ajXA== 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:cc; bh=PHz/GkGaP/x6yShTVClmF9uuG2KG9hCKZYwoVc9O428=; b=hJAe6JCsbl6SVx4nVZDhAtJMFtcPwWRqjSY7ZaLtD6cjwH6vpxAbybToDidSc+5Vms i2+JgKvsBjMWdYcPeejwZZvv7el8P+6qPvqbXI/VrlXto71Lnc5bI3Bxldkihu2tSpSc WXemm+qr2G0YvguqwTZvCNIkswGJnr4s8QnpTW6HMH+tee3D0tFbZR5jo1VHqk08p/n9 IA5rqg0Hzxm81bpJcUAW+mNyP+UxyCQeGJDEcYDvEuWCx2s5aXJLe6zDPJew5yEvFyRd m3kUoRPKyls/8jwmYtmQXtxr+qQpWQFxhtO4BuSCWnKz123biH3TmhK2oqUb2NlML60G xlwA== X-Gm-Message-State: AG10YOTAP9bz3S9zkCwRNBNTIFunLYsTwH1WpLc/PLpw1AQkun4Qaygw9NKiQ1y8WRZehYaEG7tdtZjpr8VPCw== X-Received: by 10.50.92.68 with SMTP id ck4mr6957685igb.93.1456249091169; Tue, 23 Feb 2016 09:38:11 -0800 (PST) In-Reply-To: <56CC8DBD.9080604@mail.de> 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:103410 On Tue, Feb 23, 2016 at 9:50 AM, Sven R. Kunze wrote: > On 23.02.2016 01:48, Ian Kelly wrote: >> >> On Mon, Feb 22, 2016 at 3:16 PM, Sven R. Kunze wrote: >>> >>> Is something like shown in 12:50 ( cout << tcp_reader(1000).get() ) >>> possible >>> with asyncio? (tcp_reader would be async def) >> >> loop = asyncio.get_event_loop() >> print(loop.run_until_complete(tcp_reader(1000))) > > > I see. Thanks. :) > > How come that Python (compared to C++) needs much more boilerplate to use > async programming? Historically, it was the other way round. It's not entirely clear to me what the C++ is actually doing. With Python we have an explicit event loop that has to be started to manage resuming the coroutines. Since it's explicit, you could easily drop in a different event loop, such as Tornado or curio, if desired. If your coroutine never awaits anything that isn't already done then technically you don't need an event loop, but at that point you might as well be using ordinary functions. The C++ on the other hand seems to be doing something implicit at the compiler level to make everything happen automatically inside the future.get() call, but I don't know what that is. You could wrap up the boilerplate in Python if you like: def get(coro, loop=None): if loop is None: loop = asyncio.get_event_loop() return loop.run_until_complete(coro) print(get(tcp_reader(1000)))