finalizer
This is a context manager to cleanup your environment. Your claenup code will be called even if the program is stopped by Ctrl-C or kill command.
This is useful for;
- Create a directory in the middle of the process, but you don't want it to remain when the process finished.
- You put temporary data in a database, but you don't want it to remain when the process finished.
- You want to be notified when a program terminates, whether it ends normal or dead.
- Etc...
You can also use for cleanup process for docker container, but be careful for the timeout after docker stop. (Default 10 secs)
Limitation
- This module cannot prevent from kill -9 (SIGKILL).
- Child thread in multi thread cannot handle signal. (multi_process is ok)
Install
pip install finalizer
How to Use
With with clause
Simple code
from time import sleep
from finalizer import Finalizer
def cleanup1() -> None:
print("cleanup1 start")
sleep(3)
print("cleanup1 end")
def task1() -> None:
print("task1 start")
with Finalizer(cleanup1):
sleep(3)
print("task1 end")
Output:
task1 start
cleanup1 start
cleanup1 end
task1 end
with options
def cleanup2(param1: str, param2: int) -> None:
print("cleanup2 start", param1, param2)
sleep(3)
print("cleanup2 end")
def task2() -> None:
print("task2 start")
with Finalizer(cleanup2, "test", param2=42):
sleep(3)
print("task2 end")
Output:
task2 start
cleanup2 start test 42
cleanup2 end
task2 end
you can nest with clause
def cleanup3() -> None:
print("cleanup3 start")
sleep(3)
print("cleanup3 end")
def task3() -> None:
print("task3 start")
with Finalizer(cleanup3):
sleep(3)
task1()
sleep(3)
print("task3 end")
Output:
task3 start
task1 start
cleanup1 start
cleanup1 end
task1 end
cleanup3 start
cleanup3 end
task3 end
With decorator
def cleanup4() -> None:
print("cleanup4 start")
sleep(3)
print("cleanup4 end")
@Finalizer(cleanup4)
def task4() -> None:
print("task4 start")
sleep(3)
print("task4 end")
Output:
task4 start
task4 end
cleanup4 start
cleanup4 end
How it works
Even if Ctrl-C is pressed, all cleanup will work.
>>> task3()
task3 start
task1 start <== Press Ctrl-C key here
^Ccleanup1 start
cleanup1 end
cleanup3 start
cleanup3 end
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in task3
File "<stdin>", line 4, in task1
KeyboardInterrupt
Even if Python process is killed, all cleanup will work.
>>> task3()
task3 start
task1 start <== kill python process here
cleanup1 start
cleanup1 end
cleanup3 start
cleanup3 end
While cleanup process is running, Ctrl-C or kill signal does not work.
>>> task3()
task3 start
task1 start <== Press Ctrl-C key here
^Ccleanup1 start <== Press Ctrl-C key here but does not stop
^C^C^Ccleanup1 end
cleanup3 start <== Same here, press Ctrl-C key here many times but does not stop
^C^C^C^C^C^C^C^C^C^C^Ccleanup3 end
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 5, in task3
File "<stdin>", line 4, in task1
KeyboardInterrupt
Metadata
Release files for finalizer 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| finalizer-1.0.0.tar.gz | 3.6 kB | Without provenance | Details for finalizer-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| finalizer-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for finalizer-1.0.0-py3-none-any.whl |
Total release size: 8.8 kB
Release files / finalizer-1.0.0.tar.gz
| Download URL | finalizer-1.0.0.tar.gz |
|---|---|
| Size | 3.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3a01378098ca0116bb5a8d4b2d2caa4eef1d3bbe18fedf05783c0e5f5bf0205b
|
|
BLAKE2b-256 checksum How to use checksums |
d43e4a75d95bb29eb67bc6dc9b798df9e458740c422ebc6354e7344be0b91cd6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.9.5 Linux/4.18.0
|
Release files / finalizer-1.0.0-py3-none-any.whl
| Download URL | finalizer-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
af72fbf1989605ce23bc540ce44f395fb44907659df1c00a29ca71caaffe5a56
|
|
BLAKE2b-256 checksum How to use checksums |
305b450ee105bdc3b20cb939f3be9542934a1e5f368f9b76e89d651c072676ce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.6.1 CPython/3.9.5 Linux/4.18.0
|
Security for finalizer 1.0.0
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.