<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote"><div>Changing subject line because this is way off to the side.  Guido and Nathaniel point out that you can do everything yield expressions do with async/await *without* an explicit event loop.  While I know that is true, it feels like the best case is adding fairly considerable ugliness to the code in the process.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">On Sat, Nov 25, 2017 at 3:37 PM, Guido van Rossum <<a href="mailto:guido@python.org">guido@python.org</a>> wrote:<br>> Maybe you didn't realize async/await don't need an event loop? Driving an<br>
> async/await-based coroutine is just as simple as driving a yield-from-based<br>
> one (`await` does exactly the same thing as `yield from`).<br></span></blockquote><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-">
<div class="gmail_quote">On Sun, Nov 26, 2017 at 12:29 PM, Nathaniel Smith <span dir="ltr"><<a href="mailto:njs@pobox.com" target="_blank">njs@pobox.com</a>></span> wrote:</div>
</span>Technically anything you can write with yield/yield from could also be<br>
written using async/await and vice-versa, but I think it's actually<br>
nice to have both in the language.<br></blockquote></div><div><br></div><div>Here is some code which is definitely "toy", but follows a pattern pretty similar to things I really code using yield expressions:<br><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div class="gmail_extra"><div><div><font face="monospace, monospace">In [1]: from itertools import takewhile</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">In [2]: def injectable_fib(a=1, b=2):</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:     while True:</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:         new = yield a</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:         if new is not None:</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:             a, b = new</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:         a, b = b, a+b</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">   ...:</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">In [3]: f = injectable_fib()</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">In [4]: list(takewhile(lambda x: x<200, f))</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">Out[4]: [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">In [5]: f.send((100,200))</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">Out[5]: 200</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">In [6]: list(takewhile(lambda x: x<1000, f))</font></div></div></div><div class="gmail_extra"><div><div><font face="monospace, monospace">Out[6]: [300, 500, 800]</font></div></div></div></blockquote><div class="gmail_extra"><div><br></div><div>Imagining that 'yield' vanished from the language tomorrow, and I wanted to write the same thing with async/await, I think the best I can come up with is... actually, I just don't know who to do it without any `yield`.  </div><div><br></div><div>I can get as far as a slightly flawed:</div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div><div><div><font face="monospace, monospace">In [9]: async def atakewhile(pred, coro):</font></div></div></div></div><div class="gmail_extra"><div><div><div><font face="monospace, monospace">   ...:     l = []</font></div></div></div></div><div class="gmail_extra"><div><div><div><font face="monospace, monospace">   ...:     async for x in coro:</font></div></div></div></div><div class="gmail_extra"><div><div><div><font face="monospace, monospace">   ...:         if pred(x):</font></div></div></div></div><div class="gmail_extra"><div><div><div><font face="monospace, monospace">   ...:             return l</font></div></div></div></div><div class="gmail_extra"><div><div><div><font face="monospace, monospace">   ...:         l.append(x)</font></div></div></div></div></blockquote><div class="gmail_extra"><div><br>But I just have no idea what would go in the body of </div><div><br></div></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div class="gmail_extra"><div><font face="monospace, monospace">async def afib_injectable():</font></div></div></blockquote><div class="gmail_extra"><div><br></div><div>(that is, if I'm prohibited a `yield` in there)</div><div><br></div>-- <br><div class="gmail_signature">Keeping medicines from the bloodstreams of the sick; food <br>from the bellies of the hungry; books from the hands of the <br>uneducated; technology from the underdeveloped; and putting <br>advocates of freedom in prisons.  Intellectual property is<br>to the 21st century what the slave trade was to the 16th.<br></div>
</div></div>