On Sun, Nov 26, 2017 at 7:43 PM, Chris Angelico <ros...@gmail.com> wrote:
> Honestly, this is one of Python's biggest problems when it comes to > async functions. I don't know the answer to that question, and I don't > know where in the docs I'd go looking for it. In JavaScript, async > functions are built on top of promises, so you can just say "well, you > return a promise, tada". But in Python, this isn't well documented. > Snooping the source code for asyncio.sleep() shows that it uses > @coroutine and yield, and I have no idea what magic @coroutine does, > nor how you'd use it without yield. > The source for sleep() isn't very helpful -- e.g. @coroutine is mostly a backwards compatibility thing. The heart of it is that it creates a Future and schedules a callback at a later time to complete that Future and then awaits it -- this gives control back to the scheduler and when the callback has made the Future complete, the coroutine will (eventually) be resumed. (JS Promises are equivalent to Futures, but the event loop in JS is more built in so things feel more natural there.) What we need here is not just documentation of how it works, but a good tutorial showing a pattern for writing ad-hoc event loops using a simple Future class. A good example would be some kind of parser (similar to Nathaniel's websockets example). I wish I had the time to write this example -- I have some interest in parsers that work this way (in fact I think most parsers can and probably should be written this way). But I've got a huge list of things to do already... :-( -- --Guido van Rossum (python.org/~guido)
_______________________________________________ Python-Dev mailing list Python-Dev@python.org https://mail.python.org/mailman/listinfo/python-dev Unsubscribe: https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com