Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3a.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.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'lines,': 0.07; 'matches': 0.07; "'return": 0.09; 'broke': 0.09; 'correct,': 0.09; 'front,': 0.09; 'mixed': 0.09; 'raises': 0.09; 'section,': 0.09; 'things,': 0.09; 'wrapped': 0.09; 'wrong,': 0.09; '(and,': 0.16; '*why*': 0.16; ':))': 0.16; ':-(': 0.16; 'all?': 0.16; 'coroutines': 0.16; 'frankly': 0.16; 'skipping': 0.16; 'subclass': 0.16; 'subject:between': 0.16; 'subject:tasks': 0.16; 'suspend': 0.16; 'tasks,': 0.16; 'tasks?': 0.16; '(you': 0.16; 'exception': 0.16; 'sender:addr:gmail.com': 0.17; 'wrote:': 0.18; 'bit': 0.19; 'basically': 0.19; 'result.': 0.19; 'things.': 0.19; 'examples': 0.20; 'fit': 0.20; 'seems': 0.21; 'code,': 0.22; 'to:name:python- list@python.org': 0.22; '(by': 0.24; 'instance,': 0.24; 'stick': 0.24; 'paul': 0.24; 'looks': 0.24; "i've": 0.25; 'task': 0.26; 'this:': 0.26; 'pass': 0.26; 'least': 0.26; 'somewhere': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'rest': 0.29; 'am,': 0.29; "doesn't": 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; '(which': 0.31; 'requests': 0.31; "skip:' 10": 0.31; 'another.': 0.31; 'lot.': 0.31; 'request,': 0.31; 'schedules': 0.31; 'anyone': 0.31; 'figure': 0.32; 'me?': 0.32; 'run': 0.32; 'says': 0.33; 'worked': 0.33; 'implemented': 0.33; 'sense': 0.34; 'subject:the': 0.34; 'basic': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'described': 0.36; 'done': 0.36; 'method': 0.36; 'subject:?': 0.36; 'detail': 0.37; 'clear': 0.37; 'remote': 0.38; 'tasks': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'anything': 0.39; 'use.': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'called': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'future': 0.60; 'is.': 0.60; 'hope': 0.61; 'monitoring': 0.61; 'skip:a 30': 0.61; 'traffic': 0.61; 'first': 0.61; 'back': 0.62; 'save': 0.62; 'kind': 0.63; 'more': 0.64; 'finish': 0.65; 'talking': 0.65; 'details': 0.65; 'between': 0.67; 'fulfill': 0.68; 'incoming': 0.72; 'hand': 0.80; 'future,': 0.83; '2015': 0.84; ':).': 0.84; 'asynchronous': 0.84; 'entity,': 0.84; 'impressions': 0.84; 'listener': 0.84; 'scheduling': 0.84; 'cancelled': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:content-type:content-transfer-encoding; bh=ZfEWVJ0WdloVsVZ2Q2GTaICnU6ZXFq+3aAlI/DiPZso=; b=aRTIq+eZIDbmICOCxn/Z6XhFzXVRhTu/0xFr72YDqaWWIm8YVy4z2PHh+jFO6dfvVW ZeTAg4G9wBJU9o/ccXAaQCcSlq7EfrzvJyx1jS/XmYOqJwXbYjeN7Y2qY5ykfyX1KFEv 13Wwe3omLwnC1nTedIaUiqRLyXpgIFZFmhjEpMCC8XOa/6YuOz3HgeaaWm/w2bNIFtOT TR0P+TwpOtO6e95S7DW0Xu+JlgTQ7cj/gwg5Baw+sBZVjVgAJuc/Na+m7RKvgeRrbGQa 8lRnMJbwh83vKCHKFThzHvm7HpxyoHKFiesPAlS61SxOhyq3BHKLyoaC6ZmPNmZWtMd5 2YhA== X-Received: by 10.202.195.23 with SMTP id t23mr3217679oif.117.1430842283762; Tue, 05 May 2015 09:11:23 -0700 (PDT) MIME-Version: 1.0 Sender: zachary.ware@gmail.com In-Reply-To: <344fd8f6-75c1-4b7d-888d-c5c9d4498ec3@googlegroups.com> References: <344fd8f6-75c1-4b7d-888d-c5c9d4498ec3@googlegroups.com> From: Zachary Ware Date: Tue, 5 May 2015 11:11:03 -0500 X-Google-Sender-Auth: VTMIJJmpJ18qp46SHSJRVe9lbfU Subject: Re: asyncio: What is the difference between tasks, futures, and coroutines? To: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ 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: 83 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430842286 news.xs4all.nl 2875 [2001:888:2000:d::a6]:44375 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89971 On Tue, May 5, 2015 at 10:22 AM, Paul Moore wrote: > I'm working my way through the asyncio documentation. I have got to the "= Tasks and coroutines" section, but I'm frankly confused as to the differenc= e between the various things described in that section: coroutines, tasks, = and futures. I've been using (and, in part at least, understanding :)) asyncio for work for the past few months, so I can at least give you my impressions based on use. > I think can understand a coroutine. Correct me if I'm wrong, but it's rou= ghly "something that you can run which can suspend itself". That's basically how I understand it. > But I don't understand what a Future is. The document just says it's almo= st the same as a concurrent.futures.Future, which is described as something= that "encapsulates the asynchronous execution of a callable". Which doesn'= t help a lot. In concurrent.futures, you don't create Futures, you get them= back from submit(), but in the asyncio docs it looks like you can create t= hem by hand (example "Future with run_until_complete"). And there's nothing= that says what a Future is, just what it's like... :-( My understanding is that Futures are somewhat like 'non-executable coroutines', if that makes any sense whatsoever. Futures are used as something you can pass around when you need to start execution of the "job" in one method and finish it in another. For instance, talking to a remote network entity, and you want to just do "yield from server.get(something)". Your 'get' method can make the request, create a Future, stick it somewhere that the method monitoring incoming traffic can find it (along with some identifying metadata), and then do 'return (yield from future)' to wait for the Future to be fulfilled (by the listener) and return its result. The listener then matches up incoming requests with Futures, and calls Future.set_result() or Future.set_exception() to fulfill the Future. Once one of those methods has been called on the Future (and control has been passed back to the scheduler), your server.get method unblocks from its '(yield from future)' call, and either raises the exception or returns the result that was set on the Future. > A Task is a subclass of Future, but the documentation doesn't say what it= *is*, but rather that it "schedules the execution of a coroutine". But tha= t doesn't make sense to me - objects don't do things, they *are* things. I = thought the event loop did the scheduling? > > Reading between the lines, it seems that the event loop schedules Tasks (= which makes sense) and that Tasks somehow wrap up coroutines - but I don't = see *why* you need to wrap a task in a coroutine rather than just schedulin= g coroutines. And I don't see where Futures fit in - why not just wrap a co= routine in a Future, if it needs to be wrapped up at all? You kind of mixed things up in this paragraph (you said "Tasks somehow wrap up coroutines", then "wrap a task in a coroutine"; the first is more correct, I believe). As I understand it, Tasks are specializations of Futures that take care of the set_result/set_exception based on the execution of the coroutine. I'm not clear on how that is all implemented (Are all coroutines wrapped in Tasks? Just those wrapped in asyncio.async()? No idea.), but I use it something like this: # save a reference to this task so it can be cancelled elsewhere if need= be self.current_task =3D asyncio.async(some_coroutine()) # now the coroutine is scheduled, but we still have control # we can do anything else we need to here, including scheduling other coroutines return (yield from self.current_task) > I concede that I've not read the rest of the asyncio documentation in muc= h detail yet, and I'm skipping everything to do with IO (I want to understa= nd the "async" bit for now, not so much the "IO" side). But I don't really = want to dive into the details while I am this hazy on the basic concepts. > > Can anyone clarify for me? I hope I've done some good on that front, but I'm still a bit hazy myself. I found it worked best to just take examples or otherwise working code, and poke and prod at it until it breaks and you can figure out how you broke it. Just try to avoid mixing asyncio and threads for as long as you can :). --=20 Zach