Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: [Python-ideas] How the heck does async/await work in Python 3.5 Date: Wed, 24 Feb 2016 08:41:25 -0700 Lines: 76 Message-ID: References: <56c7d145$0$1597$c3e8da3$5496439d@news.astraweb.com> <56CCC98C.5060504@mail.de> <87vb5ejfnv.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de bVU9eH+O8poN3/juGvYDSgY3XeGXT79hicDRQF4YLKRg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:: [': 0.03; 'else:': 0.03; 'exercise': 0.03; 'received:209.85.223': 0.03; 'skip:[ 20': 0.03; 'subject:Python': 0.05; 'subject:How': 0.09; 'concurrent': 0.09; 'url:github': 0.09; 'yeah,': 0.09; 'python': 0.10; 'index': 0.13; 'def': 0.13; 'wed,': 0.15; 'languages.': 0.15; 'skip:p 40': 0.15; "'from": 0.16; '2016': 0.16; '24,': 0.16; 'async': 0.16; 'concurrency': 0.16; 'coroutines': 0.16; 'descendant': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:ideas': 0.16; 'wrote:': 0.16; 'subject:] ': 0.19; 'am,': 0.23; 'feb': 0.23; 'tried': 0.24; 'import': 0.24; 'header:In- Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'actual': 0.28; 'loop,': 0.29; 'subject:/': 0.30; 'skip:[ 10': 0.31; 'point': 0.33; 'running': 0.34; 'received:google.com': 0.35; 'skip:3 10': 0.35; 'involving': 0.35; 'something': 0.35; 'level': 0.35; "isn't": 0.35; 'problem.': 0.35; 'but': 0.36; 'there': 0.36; 'received:209.85': 0.36; 'faster': 0.36; 'subject:work': 0.36; 'to:addr:python-list': 0.36; 'skip:5 10': 0.37; 'skip:4 10': 0.38; 'received:209': 0.38; 'wrong': 0.38; 'skip:p 20': 0.38; 'subject:the': 0.39; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'son': 0.61; 'here': 0.66; 'results.': 0.67; 'skip:6 10': 0.67; 'await': 0.76; 'cheating': 0.84; 'recursion:': 0.84; 'tem': 0.84; 'to:name:python': 0.84; '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:to :content-type; bh=i/5Kki2u1wnIfo4aapbzll3kssYjV167a7e5CSdn0qQ=; b=xxUEOAvRqKKjVdplTWgjV0EMLrku/xQfg2GxmXy0KBhv7t3nMcxfonswAy8dVNCPnz j8CD7COhFbx5AUAR5iL/WqPBZMFY8ctJCzqwRf1qcHS41QVlfTz3G3VrzDfN4JQaHY2i VekWVrp0jq9ED1rV9TLxg+ivciipDv+Hd28/X3smQ74ZKQ9fvSQUogsL149acAVHCoXd KDlqonMFfZExD/2fGN5nGkNKF436Fe1PsGSkjsTIev46Xz+UpYoQlTYLMbcJ97Vygzp7 Ea8O0uhPp7sSCDlZ4/7yhPRZJyQeq7CdvwEZOLxetIX5byLFN/VNkX+OCzLh/WD1YCLN bCqA== 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=i/5Kki2u1wnIfo4aapbzll3kssYjV167a7e5CSdn0qQ=; b=DfQAMENchvqvqkPqxbFj4wpkuql0nbj3Yn1Wpj38NCl/I18da7HATjZGhU9Suap0mH mUSLKuxk271bRG1UuZe/4OA74GCFlFdxzD2Ow9oOcBXvcTlPf9m3+gb9Zk4bEirKqd34 N3k7R8fOtwk4WCxYCJAXLDB85Z+keKlW44KmlX5L7eFxvBP88S7Y11tZEYSRXuPusanr +2oj0GVR1SdvpNtej4XF/4zYyRKHFr0ccPx+QYYg9Rq4ijdaAFNiAQe3IqqlDj5mdgfw mqRs65xlUtQCCrUdrJEocxfrm+bMdU0HGKgKf6uJm6KBvBrQrfWYujDA/mLpoDixJcya D3Og== X-Gm-Message-State: AG10YOSmPGnSEZHBGXgBaeOcq8bPHXFeg1rH7NZD8VYn7LNJVXxxk07nhvxdxAUKmfXaxWwLmk2O5JHkNZBwRg== X-Received: by 10.107.185.214 with SMTP id j205mr40309301iof.111.1456328525038; Wed, 24 Feb 2016 07:42:05 -0800 (PST) In-Reply-To: <87vb5ejfnv.fsf@elektro.pacujo.net> 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:103448 On Wed, Feb 24, 2016 at 8:23 AM, Marko Rauhamaa wrote: > Tem Pl : > >> Here are some concurrency benchmarks for python vs other languages. >> >> https://github.com/atemerev/skynet/pull/53 >> >> Is there something wrong with this implementation? > > It's a "fork bomb". Isn't that the point of the benchmark? But yeah, I tried playing with this and here are my results. It's clearly pushing all those coroutines down into the event loop that causes the problem. If you remove the asyncio.as_completed calls and just await the descendant coroutines in order, then it's a lot faster -- but this may be technically cheating because then the coroutines are effectively just running recursively without involving the event loop, so there's no exercise of actual concurrency. # Straight-up recursion: 400 ms def func(level=0, index=0): if level < LEVELS: sons = [func(level=level+1, index=index*SONS + x) for x in range(SONS)] sum_ = 0 for son in sons: sum_ += son return sum_ else: return index import timeit print(timeit.repeat('func()', 'from __main__ import func', number=10)) # [4.016848850995302, 4.1330014310078695, 4.149791953997919] # Concurrent coroutines with event loop: 30 s import asyncio async def coroutine(level=0, index=0): if level < LEVELS: sons = [coroutine(level=level+1, index=index*SONS + x) for x in range(SONS)] sum_ = 0 for f in asyncio.as_completed(sons): got = await f sum_ += got return sum_ else: return index print(timeit.repeat('loop.run_until_complete(coroutine())', 'from __main__ import coroutine; import asyncio; loop = asyncio.get_event_loop()', number=1)) # [29.884846250002738, 30.26590966898948, 30.716448744002264] # Recursion with coroutines: 600 ms async def coro2(level=0, index=0): if level < LEVELS: sons = [coro2(level=level+1, index=index*SONS + x) for x in range(SONS)] sum_ = 0 for son in sons: got = await son sum_ += got return sum_ else: return index print(timeit.repeat('loop.run_until_complete(coro2())', 'from __main__ import coro2; import asyncio; loop = asyncio.get_event_loop()', number=1)) # [0.6264017040084582, 0.590631059021689, 0.5807875629980117] # Recursive coroutines, no event loop: 600 ms print(timeit.repeat('list(coro2().__await__())', 'from __main__ import coro2', number=10)) # [6.028770218021236, 6.002665672975127, 5.987304503010819]