Package commons :: Module control
[hide private]
[frames] | no frames]

Source Code for Module commons.control

 1  # -*- mode: python; tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4; -*- 
 2  # vim:ft=python:et:sw=4:ts=4 
 3   
 4  """Processes, idling, and signals.""" 
 5   
 6  from logging import * 
 7  import os, signal, time 
8 9 -def suicide():
10 """Force-kill the current process.""" 11 os.kill( os.getpid(), signal.SIGKILL )
12
13 ############################################################################# 14 15 -class Sleeper( object ):
16 """Sleeper class. Goes to sleep forever.""" 17 @classmethod
18 - def sleep( cls ):
19 """ 20 Calls C{time.sleep} for 100000 seconds at a time in a 21 while-true loop. 22 """ 23 while True: 24 time.sleep( 100000 )
25
26 ############################################################################# 27 28 -class SigTerm( Exception ): pass
29
30 -def handle_signal( signum, frame ):
31 """ 32 Generic signal handler that logs the received signal to the 33 "signal" logging channel (log level INFO) and then raises 34 L{SigTerm} if it was not previously raised. 35 36 @param signum: The signal's numeric value (see C{man 7 signal}). 37 38 @param frame: Ignored. 39 40 @raise SigTerm: Only once. 41 """ 42 global got_signal 43 info( 'signal', 'got signal', str( signum ) ) 44 if not got_signal: 45 got_signal = True 46 raise SigTerm
47
48 -def handle_signals( *sigs ):
49 """ 50 For each given signal, register the L{handle_signal} as its 51 handler. 52 53 @param sigs: The set of sigs to iterate over. 54 @type sigs: set 55 """ 56 global got_signal 57 got_signal = False 58 for sig in sigs: 59 signal.signal( signal.SIGTERM, handle_signal )
60