Signal handling API¶
-
class
SignalHandler1(pid_file, sigint=None, sigterm=None, sigabrt=None)¶ New in version 0.1.
Handles signals for the daemonized process.
Parameters: - pid_file (str) – The path to the PID file.
- sigint – A callable handler for the
SIGINTsignal. May also bedaemoniker.IGNORE_SIGNALto explicitly ignore the signal. Passing the default value ofNonewill assign the defaultSIGINThandler, which will simplyraise daemoniker.SIGINTwithin the main thread. - sigterm – A callable handler for the
SIGTERMsignal. May also bedaemoniker.IGNORE_SIGNALto explicitly ignore the signal. Passing the default value ofNonewill assign the defaultSIGTERMhandler, which will simplyraise daemoniker.SIGTERMwithin the main thread. - sigabrt – A callable handler for the
SIGABRTsignal. May also bedaemoniker.IGNORE_SIGNALto explicitly ignore the signal. Passing the default value ofNonewill assign the defaultSIGABRThandler, which will simplyraise daemoniker.SIGABRTwithin the main thread.
Warning
There is a slight difference in handler calling between Windows and Unix systems. In every case, the default handler will always
raisefrom within the main thread. However, if you define a custom signal handler, on Windows systems it will be called from within a daughter thread devoted to signal handling. This has two consequences:- All signal handlers must be threadsafe
- On Windows, future signals will be silently dropped until the callback completes
On Unix systems, the handler will be called from within the main thread.
Note
On Windows,
CTRL_C_EVENTandCTRL_BREAK_EVENTsignals are both converted toSIGINTsignals internally. This also applies to custom signal handlers.>>> from daemoniker import SignalHandler1 >>> sighandler = SignalHandler1('pid.pid')
-
sigint¶ The current handler for
SIGINTsignals. This must be a callable. It will be invoked with a single argument: the signal number. It may be re-assigned, even after callingstart(). Deleting it will restore the defaultDaemonikersignal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNALas the handler.
-
sigterm¶ The current handler for
SIGTERMsignals. This must be a callable. It will be invoked with a single argument: the signal number. It may be re-assigned, even after callingstart(). Deleting it will restore the defaultDaemonikersignal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNALas the handler.
-
sigabrt¶ The current handler for
SIGABRTsignals. This must be a callable. It will be invoked with a single argument: the signal number. It may be re-assigned, even after callingstart(). Deleting it will restore the defaultDaemonikersignal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNALas the handler.
-
start()¶ Starts signal handling. Must be called to receive signals with the
SignalHandler.
-
stop()¶ Stops signal handling. Must be called to stop receive signals.
stopwill be automatically called:- at the interpreter exit, and
- when the main thread exits.
stopis idempotent. On Unix systems, it will also restore the previous signal handlers.
-
IGNORE_SIGNAL¶ A constant used to explicitly declare that a
SignalHandler1should ignore a particular signal.
-
send(pid_file, signal)¶ New in version 0.1.
Send a
signalto the process atpid_file.Parameters: - pid_file (str) – The path to the PID file.
- signal –
The signal to send. This may be either:
- an instance of one of the
ReceivedSignalexceptions, for example:daemoniker.SIGINT()(seeSIGINT) - the class for one of the
ReceivedSignalexceptions, for example:daemoniker.SIGINT(seeSIGINT) - an integer-like value, corresponding to the signal number, for
example:
signal.SIGINT
- an instance of one of the
>>> from daemoniker import send >>> from daemoniker import SIGINT >>> send('pid.pid', SIGINT)