Skip to content
An easy way to ensure that a program will exit in a safe fashion
Find file
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


An easy way to ensure that a python program will exit in safe fashion.

(Custom signal handlers for SIGTERM and CTRL-C events)


I realized that this package is needed when I was thinking about the best way to restart
brackground workers in a graceful manner, without loosing data.

The basic idea is that a worker should exit on the next data processing cycle 
and the exit event should not break current program execution.

I also believe that it's really bad idea to handle signals as exceptions 
(e.g. KeyboardInterrupt) because you can't resume program execution and it's not
always possible to do a rollback (talking to external services).


# Basic worker loop

import safeclose

with safeclose.section():
  while not safeclose.exit():
    # do work and never block for ever

# Tornado web application

import tornado

def onexit():

with safeclose.section(onexit):

safeclose.exit() should return True if the program receives SIGTERM or SIGINT (CTRL-C).


Copyright 2011 Andrei Savu
Apache License 2.0

Something went wrong with that request. Please try again.