Skip to main content

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.

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

finalizer-1.0.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

finalizer-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file finalizer-1.0.0.tar.gz.

File metadata

  • Download URL: finalizer-1.0.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.9.5 Linux/4.18.0

File hashes

Hashes for finalizer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 3a01378098ca0116bb5a8d4b2d2caa4eef1d3bbe18fedf05783c0e5f5bf0205b
MD5 82e8dd81a43285128adce905945079bd
BLAKE2b-256 d43e4a75d95bb29eb67bc6dc9b798df9e458740c422ebc6354e7344be0b91cd6

See more details on using hashes here.

File details

Details for the file finalizer-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: finalizer-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.9.5 Linux/4.18.0

File hashes

Hashes for finalizer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 af72fbf1989605ce23bc540ce44f395fb44907659df1c00a29ca71caaffe5a56
MD5 64706fa27d970c6a61b25533da2e084f
BLAKE2b-256 305b450ee105bdc3b20cb939f3be9542934a1e5f368f9b76e89d651c072676ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page