Skip to main content

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)

Table of built distributions (wheels) for postscriptum 0.3
File Interpreter ABI Platform
postscriptum-0.3-py3-none-any.whl Python 3 none any Details

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

Release history Release notifications | RSS feed

This release

0.3 This release

1 release file

0.2

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page