<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Jun 8, 2016 at 9:38 PM, Andrew Svetlov <span dir="ltr"><<a href="mailto:andrew.svetlov@gmail.com" target="_blank">andrew.svetlov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr">My first thought was to use `async with` but I found that just `with` also works.</div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div dir="ltr"><br>Sorry, but I don't follow how async context manager may be better.</div></blockquote><div><br></div><div>`async with` communicates directly with the coroutine runner. Plain `with` requires that the coroutine runner be discoverable in some other way (via a global). Either way, for `asyncio.timeout` to be portable to other coroutine runners, we must define a protocol that coroutine runners may implement.<br></div><div><br></div><div>If we use `async with`, that protocol is something like "if the coroutine yields an instance of asyncio.TimeoutSetter, the runner should set its timeout accordingly".</div><div><br></div><div>If we use plain `with`, we would need to move asyncio.Task._current_task somewhere public (an attribute of EventLoop? a global threading.local?) and the protocol would be that each coroutine runner must register itself there while it is on the top of the stack and implement a `set_timeout()` method. </div><div><br></div><div>I believe the `async with` solution will perform better (an isinstance() check on every yielded object, plus one extra yielded object when the timeout feature is used, versus at least two operations on a central registry every time the coroutine runner enters or leaves the stack), and it also degrades more gracefully (the TimeoutSetter object could be a subclass of Future that resolves instantly so runners that don't understand it can simply ignore it. A runner that doesn't participate in the current_task registry may leave the wrong task at the top of the stack).</div><div><br></div><div>-Ben</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><div class=""><div class="h5"><br><div class="gmail_quote"><div dir="ltr">On Wed, Jun 8, 2016 at 10:49 AM Yury Selivanov <<a href="mailto:yselivanov@gmail.com" target="_blank">yselivanov@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex"><br>
> On Jun 8, 2016, at 1:15 PM, Andrew Svetlov <<a href="mailto:andrew.svetlov@gmail.com" target="_blank">andrew.svetlov@gmail.com</a>> wrote:<br>
><br>
> asyncio.timeout behaves like asyncio.wait_for BTW.<br>
> It cancels inner tasks and sends asyncio.TimeoutError to outer.<br>
<br>
Right.  And I think it’s a totally normal behaviour for asyncio.<br>
<br>
The only thing I’d fix in asyncio.timeout() (aside from resolving<br>
Ben’s questions) is to make it use ‘async with’ statement.<br>
<br>
Yury<br>
<br>
</blockquote></div></div></div><span class=""><font color="#888888"><div dir="ltr">-- <br></div><div data-smartmail="gmail_signature"><div dir="ltr"><div>Thanks,</div>Andrew Svetlov</div></div>
</font></span><br>_______________________________________________<br>
Async-sig mailing list<br>
<a href="mailto:Async-sig@python.org">Async-sig@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/async-sig" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/async-sig</a><br>
Code of Conduct: <a href="https://www.python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">https://www.python.org/psf/codeofconduct/</a><br></blockquote></div><br></div></div>