Postscriptum: an unified API to run code when Python exits
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.
Metadata
Release files for postscriptum 0.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| postscriptum-0.3-py3-none-any.whl | Python 3 | none | any | Details for postscriptum-0.3-py3-none-any.whl |
Release files / postscriptum-0.3-py3-none-any.whl
| Download URL | postscriptum-0.3-py3-none-any.whl |
|---|---|
| Size | 13.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
a6994fbc486344d620cb2e964bdd1aea717be79394ec7853373ad8e6584d9f68
|
|
BLAKE2b-256 checksum How to use checksums |
21b8bf4ffc2ad28ef51f4e891122d114797ec3137bcf9f70d34a2a1270cf3100
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.7
|
Security for postscriptum 0.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.