Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.026 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'essentially': 0.04; 'debug': 0.07; 'callback': 0.09; 'explanation': 0.09; 'idea?': 0.09; 'initiating': 0.09; 'pep': 0.09; 'python': 0.11; 'suggest': 0.14; 'btw': 0.16; "module's": 0.16; 'sequential': 0.16; 'tasks?': 0.16; 'wrote:': 0.18; 'library': 0.18; 'looked': 0.18; 'module': 0.19; 'trying': 0.19; 'normally': 0.19; 'thoughts': 0.19; 'example': 0.22; 'programming': 0.22; 'handles': 0.22; 'lets': 0.24; 'mon,': 0.24; 'looks': 0.24; 'task': 0.26; 'gets': 0.27; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'thus': 0.29; 'related': 0.29; '(like': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'url:wiki': 0.31; 'block,': 0.31; 'complete,': 0.31; 'initiate': 0.31; 'url:wikipedia': 0.31; 'anyone': 0.31; 'allows': 0.31; 'quite': 0.32; 'sense': 0.34; 'maybe': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'introducing': 0.36; 'url:org': 0.36; 'should': 0.36; 'to:addr:python-list': 0.38; 'does': 0.39; "couldn't": 0.39; 'weight': 0.39; 'to:addr:python.org': 0.39; 'called': 0.40; 'read': 0.60; 'simple': 0.61; "you're": 0.61; 'here:': 0.62; 'such': 0.63; 'more': 0.64; 'great': 0.65; 'between': 0.67; 'tasks.': 0.68; 'stated': 0.69; 'risk': 0.72; 'saw': 0.77; 'url:o': 0.78; 'asynchronous': 0.84; 'confusing': 0.84; 'existence.': 0.84; 'task,': 0.91; 'directly.': 0.95; 'race': 0.95 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:content-transfer-encoding; bh=4PUeNl+ERbpdmnBeKG7R7kA2q03BXo4FIhHubLCgiys=; b=NOQELevx5KmbBAKWochbQhq4rWtxTpnRpmpab1b632whoE09gp6SAu5LMPKnRRvtdi j+Iyq8ZQ5DUnxMqFuFWUaVAebFINcqiJLWdZ0itf9/K36FYl5ZodTCcUAEy3JGMlciln wXabhYoS70zbTJqzPeKY0yVScdG9q0n8gk8Y6gm0NqvEDoT7AF/NYmb5RnnR4xG/Ze2K 1DdwwMxK2kcXQzEhn+v7z9kwrGgI3dpmcd8zGSUZeh0nvxKpJGCjApqO+9J+mEafPMfo sw8jxWRW6y+0nh5wZDIB4a+py/Cxwi2395PrF0FqtYBzu++mAX1JSzJM8lk8CLflExxl qvSg== X-Received: by 10.236.13.46 with SMTP id a34mr54737266yha.28.1401734315736; Mon, 02 Jun 2014 11:38:35 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Mon, 2 Jun 2014 12:37:55 -0600 Subject: Re: Benefits of asyncio To: Python Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 34 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1401734686 news.xs4all.nl 2956 [2001:888:2000:d::a6]:53113 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:72438 On Mon, Jun 2, 2014 at 11:40 AM, Aseem Bansal wrote: > I read in these groups that asyncio is a great addition to Python 3. I ha= ve looked around and saw the related PEP which is quite big BTW but couldn'= t find a simple explanation for why this is such a great addition. Any simp= le example where it can be used? > > It can be used to have a queue of tasks? Like threads? Maybe light weight= threads? Those were my thoughts but the library reference clearly stated t= hat this is single-threaded. So there should be some waiting time in betwee= n the tasks. Then what is good? > > These are just jumbled thoughts that came into my mind while trying to ma= ke sense of usefulness of asyncio. Anyone can give a better idea? You're right, neither the PEP nor the docs to much to motivate the module's existence. I suggest you start here: http://en.wikipedia.org/wiki/Asynchronous_I/O The asynchronous model lets you initiate a task (typically an I/O task) that would normally block, and then go on to do other things (like initiating more tasks) while waiting on that task, without having to resort to multiple threads or processes (which have the disadvantages of consuming more system resources as well as introducing the risk of race conditions and deadlocks). It does this by using callbacks; when a task is complete, a callback is called that handles its completion. Often in asynchronous code you end up with large networks of callbacks that can be confusing to follow and debug because nothing ever gets called directly. One of the significant features of the asyncio module is that it allows asynchronous programming using coroutines, where the callbacks are abstracted away and essentially have the effect of resuming the coroutine when the task completes. Thus you end up writing code that looks a lot like threaded, sequential code with none of the pitfalls.