<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 18, 2015 at 10:25 AM, Szieberth Ádám <span dir="ltr"><<a href="mailto:sziebadam@gmail.com" target="_blank">sziebadam@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Thanks for your reply Guido!<br>
<span class=""><br>
> - Instead of calling signal.signal() yourself, you should use<br>
> loop.add_signal_handler(). It makes sure your signal handler doesn't run<br>
> while another handler is already running.<br>
<br>
</span>I was opted to the signal module because `signal` documentation suggest that<br>
it alos supports Windows while asyncio documentation states that `loop.<br>
add_signal_handler()` is UNIX only.<span class=""><br></span></blockquote><div><br></div><div>Unfortunately that's true, but using the signal module with asyncio the way you did is *not* safe. The only safe way is to use the loop.add_signal_handler() interface.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> - I'm unclear on why you want a wait_forever() instead of using<br>
> loop.run_forever(). Can you clarify?<br>
<br>
</span>As I see `loop.run_forever()` is an issue from _outside_ while an `await<br>
wait_forever()` would be an _inside_ declaration making explicit what the task<br>
does (serving forever).<br>
<br>
My OP suggest that it seemed to me quite helpful inside async context.<br>
However, I wanted to share my approach to get a confirmation that I am not on<br>
a totally wrong way with this.<span class=""><br></span></blockquote><div><br></div><div>Well, if you look at the toy servers in the asyncio examples directory, they all use run_forever(). I agree that from within the loop that's not possible, but I don't think it's such a common thing (you typically write a framework for creating servers once and that's the only place where you would need this). IOW I think your solution of waiting for a Future is the right way.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> - In theory, instead of waiting for a Future that is cancelled by a<br>
> handler, you should be able to use asyncio.sleep() with a very large number<br>
> (e.g. a million seconds).<br>
<br>
</span>I was thinking on this too but it seemed less explicit to me than awaiting a<br>
pure Future with a short comment. Moreover, even millions of seconds can pass.<span class=""><br></span></blockquote><div><br></div><div>11 years. That's quite some trust you put in your hardware... But you can use a billion. I think by 11000 years from now you can retire your server. :-)<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> Your handler could then just call loop.stop().<br>
<br>
</span>For some reason I don't like bothering with the event loop from inside<br>
awaitables. It seems hacky to me since it breaks the hierarhy of who controlls<br>
who.<span class=""><br></span></blockquote><div><br></div><div>Fair enough -- you've actually internalized the asyncio philosophy quite well.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> However, I just tested this and it raises "RuntimeError: Event loop stopped<br>
> before Future completed." so ignore this until we've fixed it. :-)<br>
<br>
</span>This is the exception I saw so many times by trying to close an asyncio<br>
program! I guess I am not the only one. This may be one of the most<br>
frustrating aspects of the library. Yet, it inspired me to figure out a plain<br>
pattern to avoid it, which may not the right one. However, I would like to<br>
signal that it would be nice to help developers with useful patterns and<br>
documentation to avoid RuntimeErrors and the frustration that goes with them.<br></blockquote><div><br></div><div>Maybe you can help by submitting a patch that prevents this error! Are you interested?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Ádám<br>
(<a href="http://szieberthadam.github.io/" rel="noreferrer" target="_blank">http://szieberthadam.github.io/</a>)<br>
<br>
PS: I will replay to others as well, but first I had to play with my son. :)<br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>