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
SIGINT
signal. May also bedaemoniker.IGNORE_SIGNAL
to explicitly ignore the signal. Passing the default value ofNone
will assign the defaultSIGINT
handler, which will simplyraise daemoniker.SIGINT
within the main thread. - sigterm – A callable handler for the
SIGTERM
signal. May also bedaemoniker.IGNORE_SIGNAL
to explicitly ignore the signal. Passing the default value ofNone
will assign the defaultSIGTERM
handler, which will simplyraise daemoniker.SIGTERM
within the main thread. - sigabrt – A callable handler for the
SIGABRT
signal. May also bedaemoniker.IGNORE_SIGNAL
to explicitly ignore the signal. Passing the default value ofNone
will assign the defaultSIGABRT
handler, which will simplyraise daemoniker.SIGABRT
within 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
raise
from 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_EVENT
andCTRL_BREAK_EVENT
signals are both converted toSIGINT
signals internally. This also applies to custom signal handlers.>>> from daemoniker import SignalHandler1 >>> sighandler = SignalHandler1('pid.pid')
-
sigint
¶ The current handler for
SIGINT
signals. 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 defaultDaemoniker
signal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNAL
as the handler.
-
sigterm
¶ The current handler for
SIGTERM
signals. 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 defaultDaemoniker
signal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNAL
as the handler.
-
sigabrt
¶ The current handler for
SIGABRT
signals. 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 defaultDaemoniker
signal handler; to ignore it, instead assigndaemoniker.IGNORE_SIGNAL
as 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.
stop
will be automatically called:- at the interpreter exit, and
- when the main thread exits.
stop
is idempotent. On Unix systems, it will also restore the previous signal handlers.
-
IGNORE_SIGNAL
¶ A constant used to explicitly declare that a
SignalHandler1
should ignore a particular signal.
-
send
(pid_file, signal)¶ New in version 0.1.
Send a
signal
to 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
ReceivedSignal
exceptions, for example:daemoniker.SIGINT()
(seeSIGINT
) - the class for one of the
ReceivedSignal
exceptions, 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)