Popular Python recipes tagged "exit"http://code.activestate.com/recipes/langs/python/tags/exit/2016-05-31T00:42:47-07:00ActiveState Code RecipesRegister exit function (Python)
2016-05-31T00:42:47-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/580672-register-exit-function/
<p style="color: grey">
Python
recipe 580672
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/signal/">signal</a>).
Revision 3.
</p>
<p>This is a function / decorator which registers a function which will be executed on "normal" interpreter exit or in case one of the <code>signals</code> is received by this process (differently from atexit.register()). Also, it makes sure to execute any other function which was previously registered via signal.signal(). If any, it will be executed after our own <code>fun</code>. The full blogpost explaining why you should use this instead of atexit module is here: <a href="http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html" rel="nofollow">http://grodola.blogspot.com/2016/02/how-to-always-execute-exit-functions-in-py.html</a></p>
Handle exit context manager (Python)
2014-08-01T08:28:07-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577997-handle-exit-context-manager/
<p style="color: grey">
Python
recipe 577997
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/atexit/">atexit</a>, <a href="/recipes/tags/contextlib/">contextlib</a>, <a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python3/">python3</a>, <a href="/recipes/tags/shutdown/">shutdown</a>, <a href="/recipes/tags/sigint/">sigint</a>, <a href="/recipes/tags/signal/">signal</a>, <a href="/recipes/tags/sigterm/">sigterm</a>).
Revision 23.
</p>
<p>A context manager which properly handles SIGTERM (SystemExit) and SIGINT (KeyboardInterrupt) signals, registering a function which is always guaranteed to be called on interpreter exit.
Also, it makes sure to execute previously registered functions as well (if any).</p>
Wait for PID and check for PID existance (POSIX) (Python)
2012-04-15T14:13:02-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578022-wait-for-pid-and-check-for-pid-existance-posix/
<p style="color: grey">
Python
recipe 578022
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/exists/">exists</a>, <a href="/recipes/tags/exit/">exit</a>, <a href="/recipes/tags/pid/">pid</a>, <a href="/recipes/tags/process/">process</a>, <a href="/recipes/tags/status/">status</a>, <a href="/recipes/tags/timeout/">timeout</a>, <a href="/recipes/tags/wait/">wait</a>).
Revision 7.
</p>
<p>Two functions: one which waits for a certain PID to terminate (with optional timeout), another one which checks whether a process with a given PID is running.</p>
<p>This was extracted from <a href="http://code.google.com/p/psutil/">psutil</a> project which also provides a Windows implementation.</p>
<p>Timeout functionality is implemented as a busy loop and it was inspired by <a href="http://bugs.python.org/issue5673." rel="nofollow">http://bugs.python.org/issue5673.</a></p>