Improve signal handling

This commit is contained in:
Jannes Höke 2015-11-23 17:40:39 +01:00
parent 0b72acc7c8
commit 1005ad57ce
2 changed files with 13 additions and 6 deletions

View file

@ -67,7 +67,8 @@ def main():
# Start the Bot
updater.start_polling(timeout=5)
# Run the bot until the user presses Ctrl-C
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT
updater.idle()
if __name__ == '__main__':

View file

@ -234,11 +234,17 @@ class Updater:
self.is_idle = False
self.stop()
def idle(self):
""" Waits for the user to press Ctrl-C and stops the updater """
signal(SIGINT, self.signal_handler)
signal(SIGTERM, self.signal_handler)
signal(SIGABRT, self.signal_handler)
def idle(self, stop_signals=(SIGINT, SIGTERM, SIGABRT)):
"""
Waits for the user to press Ctrl-C and stops the updater
Args:
stop_signals: Iterable containing signals from the signal module
that should be subscribed to. Updater.stop() will be called on
receiving one of those signals.
"""
for sig in stop_signals:
signal(sig, self.signal_handler)
self.is_idle = True