simplesignals

Unix signal handlers and worker processes, simplified
Download

simplesignals Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Public Domain
  • Price:
  • FREE
  • Publisher Name:
  • Jamie Matthews
  • Publisher web site:
  • https://github.com/j4mie/

simplesignals Tags


simplesignals Description

simplesignals is a Python module that provides Unix signal handlers and worker processes, simplified.DocumentationThis project provides a layer on top of the built-in signal module. It allows you to easily register functions as handlers for Unix signals. It also provides a lightweight base class for implementing basic Unix worker processes.Signal handlersThe library provides a set of function decorators, one for each signal type, which you can use to declare a function as a signal handler. For example, to connect a handler function to the SIGINT signal:from simplesignals import signals@signals.intdef handler(): print "Bye then!" exit()If you want to use the same function to handle multiple signals, you can stack the decorators and ask that the signal be passed on to your handler function:from simplesignals import signals@signals.int(takes_signal=True)@signals.term(takes_signal=True)@signals.quit(takes_signal=True)def handler(signal): if signal == signals.int: print "Got int!" # ... etc exit()If you need the execution frame that would be handed to your function by the signal module, you can ask for that too:from simplesignals import signals@signals.quit(takes_frame=True)def handler(frame): # do something with frame exit()System call interrupt behaviour can be controlled with the allow_interrupt flag. See the signal module docs for details.Worker processesOne of the primary uses of Unix signals is to implement well-behaved worker processes. Process management tools such as Circus use signals to communicate with your processes. If you can handle signals correctly, you can take the opportunity to gracefully shut down your process and avoid half-finished jobs, etc.An extremely simple base class is provided which currently provides the following:- A main loop that allows your process to perform its work.- Graceful shutdown on SIGINT, SIGTERM and SIGQUIT.- Sets the title of the process if setproctitle is installed.A simple example:from simplesignals.process import WorkerProcessBaseclass MyWorker(WorkerProcessBase): process_title = "my-worker" def do_work(self): # this method is called repeatedly, do your work here # eg. get item from a queue and process it self.do_some_really_super_hard_work()if __name__ == "__main__": worker = Worker() worker.run()Please take a look at the source code to understand exactly what functionality WorkerProcessBase provides.Product's homepage


simplesignals Related Software