An unified API to run code when Python exits
Project description
Postscriptum wraps atexit.register, sys.excepthook and signal.signal to lets you do:
from postscriptum import PubSub ps = PubSub() # do this before creating a thread or a process @ps.on_finish() # don't forget the parenthesis ! def _(event): print("When the program finishes, no matter the reason.") @ps.on_terminate() def _(event): # event contains the signal that lead to termination print("When the user terminates the program. E.G: Ctrl + C, kill -9, etc.") @ps.on_crash() def _(event): # event contains the exception and traceback print("When there is an unhandled exception") ps.start()
All those functions will be called automatically at the proper moment. The handler for on_finish will be called even if another handler has been called.
Install
pip install postscriptum
Coverage: 100%
Tested: Linux, but should work on Windows and Mac
Fully typed hint
Why this lib ?
Python has 3 very different API to deal with exiting, and they all have their challenges:
atexit: the handler is always called, weither python exited cleanly or not, which can lead do duplicated calls. Except if you get a SIGTERM signal when it’s silently ignored. Even whell called, it doesn’t give any information on the cause of the exit.
signal: to capture terminating signals, you need to know which ones to watch for (and they differ depending of the OS). Normal behavior is to exit, but if you set your handler, the program will not exit unless you call sys.exit(). What’s more, you can only have one handler for each signal. Finally, there are gotchas when using I/O inside a handler.
sys.excepthool is called on all exception, but not SystemExit. It also leads to hard to debug errors if you don’t call the previous hook properly. And you can have only one handler.
Also, there is no automatic way to react to sys.exit(). And no way to distinguish SystemExit from sys.exit(), which you need for signals.
Postscriptum doesn’t deal with the last goatchas yet:
signals are caught by childs and passed to the main threads, but not exceptions.
messing up in your handler may cause you to have no error message at all.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
File details
Details for the file postscriptum-0.3-py3-none-any.whl
.
File metadata
- Download URL: postscriptum-0.3-py3-none-any.whl
- Upload date:
- Size: 13.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a6994fbc486344d620cb2e964bdd1aea717be79394ec7853373ad8e6584d9f68 |
|
MD5 | 102613ca9c5782683d234ee657a2b280 |
|
BLAKE2b-256 | 21b8bf4ffc2ad28ef51f4e891122d114797ec3137bcf9f70d34a2a1270cf3100 |