Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'yet.': 0.04; 'lines,': 0.07; 'callback': 0.09; 'expected.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'section,': 0.09; 'things,': 0.09; 'wrapped': 0.09; 'wrong,': 0.09; 'jan': 0.12; '*why*': 0.16; ':-(': 0.16; 'all?': 0.16; 'coroutines': 0.16; 'frankly': 0.16; 'helps.': 0.16; 'occurs,': 0.16; 'placeholder': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'subclass': 0.16; 'subject:between': 0.16; 'subject:tasks': 0.16; 'suspend': 0.16; 'tasks,': 0.16; 'wrote:': 0.18; 'things.': 0.19; 'fit': 0.20; 'thanks.': 0.20; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'paul': 0.24; 'looks': 0.24; "i've": 0.25; 'task': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; '(which': 0.31; 'lot.': 0.31; 'schedules': 0.31; 'yields': 0.31; 'allows': 0.31; 'themselves': 0.32; 'run': 0.32; 'says': 0.33; 'sense': 0.34; 'subject:the': 0.34; 'anywhere': 0.35; 'something': 0.35; 'objects': 0.35; 'but': 0.35; 'described': 0.36; 'interaction': 0.36; 'done': 0.36; 'subject:?': 0.36; 'easily': 0.37; 'tasks': 0.38; 'to:addr:python- list': 0.38; 'pm,': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'called': 0.40; 'future': 0.60; 'event.': 0.60; 'ian': 0.60; 'is.': 0.60; 'hope': 0.61; 'helps': 0.61; 'back': 0.62; 'between': 0.67; 'completion': 0.78; 'hand': 0.80; 'future,': 0.83; '2015': 0.84; 'asynchronous': 0.84; 'received:fios.verizon.net': 0.84; 'scheduling': 0.84; 'task,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: asyncio: What is the difference between tasks, futures, and coroutines? Date: Tue, 05 May 2015 18:38:41 -0400 References: <344fd8f6-75c1-4b7d-888d-c5c9d4498ec3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-98-114-97-173.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 In-Reply-To: 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: 41 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430865534 news.xs4all.nl 2880 [2001:888:2000:d::a6]:41701 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90007 On 5/5/2015 1:46 PM, Ian Kelly wrote: > On Tue, May 5, 2015 at 9: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 difference between the various things described in that section: coroutines, tasks, and futures. >> >> I think can understand a coroutine. Correct me if I'm wrong, but it's roughly "something that you can run which can suspend itself". >> >> But I don't understand what a Future is. The document just says it's almost 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 them by hand (example "Future with run_until_complete"). And there's nothing that says what a Future is, just what it's like... :-( > > Fundamentally, a future is a placeholder for something that isn't > available yet. You can use it to set a callback to be called when that > thing is available, and once it's available you can get that thing > from it. > >> 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 that doesn't make sense to me - objects don't do things, they *are* things. I thought the event loop did the scheduling? > > In asyncio, a Task is a a Future that serves as a placeholder for the > result of a coroutine. The event loop manages callbacks, and that's > all it does. An event that it's been told to listen for occurs, and > the event loop calls the callback associated with that event. The Task > manages a coroutine's interaction with the event loop; when the > coroutine yields a future, the Task instructs the event loop to listen > for the completion of that future, setting a callback that will resume > the coroutine. > >> 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 scheduling coroutines. And I don't see where Futures fit in - why not just wrap a coroutine in a Future, if it needs to be wrapped up at all? > > The coroutines themselves are not that interesting of an interface; > all you can do with them is resume them. The asynchronous execution > done by asyncio is all based on futures. Because a coroutine can > easily be wrapped in a Task, this allows for coroutines to be used > anywhere a future is expected. > > I don't know if I've done a good job explaining, but I hope this helps. It helps me. Thanks. -- Terry Jan Reedy