<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Feb 12, 2016 at 7:11 PM, Ethan Furman <span dir="ltr"><<a href="mailto:ethan@stoneleaf.us" target="_blank">ethan@stoneleaf.us</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">On 02/12/2016 09:51 AM, Giampaolo Rodola' wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
http://...<br>
Thoughts?<br>
</blockquote>
<br>
If you have an idea, please post that idea here, not a link.<br>
<br>
--<br>
~Ethan~<br>
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</blockquote></div><div class="gmail_signature"><div dir="ltr"><div><br></div><div>Of course you are right, I'm sorry.</div><div>So, one problem with atexit.register() is that it won't run the registered function in case the process is killed by a signal.</div><div><br></div><div><div>import atexit, os, signal</div><div>@atexit.register</div><div>def cleanup():</div><div>    print("on exit")  # XXX this never gets printed</div><div>os.kill(os.getpid(), signal.SIGTERM)</div></div><div><br></div><div>The correct way to do that is to use signal.signal(). The problem with that though is that in case a third-party module has </div><div>already registered a function for that signal, the new function will overwrite the old one:</div><div><br><div>import os, signal</div><div><br></div><div>def old(*args):</div><div>    print("old")  # XXX this never gets printed</div><div><br></div><div>def new(*args):</div><div>    print("new")</div><div><br></div><div>signal.signal(signal.SIGTERM, old)</div><div>signal.signal(signal.SIGTERM, new)</div><div>os.kill(os.getpid(), signal.SIGTERM)</div></div><div> </div><div>Also, we would still have to use atexit.register() so that the new function is called also on "clean" interpreter exit and take into account other signals other than SIGTERM which would cause the process to terminate (SIGINT, etc.). My proposal is to evaluate adding a higher-level function which takes care of all these details, providing better chances to execute the exit function than atexit.register or signal.signal alone.</div><div><div>I also noticed that every time I end up working on a new code base, most of the time people are using either atexit or signal module without knowing these implications. It also took quite a bit to do the same and come up with this code.</div><div><br></div></div><div>Code:<br><a href="https://gist.github.com/giampaolo/6f07f6cd7dd1c667b0bb">https://gist.github.com/giampaolo/6f07f6cd7dd1c667b0bb</a></div><div><br>Relevant blog post:<br></div><div><a href="http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html">http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html</a><br></div><div><br></div><div>Thoughts?</div></div></div>
</div></div>