Skip to content

Commit 1005ad5

Browse files
committed
Improve signal handling
1 parent 0b72acc commit 1005ad5

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

examples/eventhandler_simplebot.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ def main():
6767
# Start the Bot
6868
updater.start_polling(timeout=5)
6969

70-
# Run the bot until the user presses Ctrl-C
70+
# Run the bot until the user presses Ctrl-C or the process receives SIGINT,
71+
# SIGTERM or SIGABRT
7172
updater.idle()
7273

7374
if __name__ == '__main__':

telegram/updater.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,17 @@ def signal_handler(self, signum, frame):
234234
self.is_idle = False
235235
self.stop()
236236

237-
def idle(self):
238-
""" Waits for the user to press Ctrl-C and stops the updater """
239-
signal(SIGINT, self.signal_handler)
240-
signal(SIGTERM, self.signal_handler)
241-
signal(SIGABRT, self.signal_handler)
237+
def idle(self, stop_signals=(SIGINT, SIGTERM, SIGABRT)):
238+
"""
239+
Waits for the user to press Ctrl-C and stops the updater
240+
241+
Args:
242+
stop_signals: Iterable containing signals from the signal module
243+
that should be subscribed to. Updater.stop() will be called on
244+
receiving one of those signals.
245+
"""
246+
for sig in stop_signals:
247+
signal(sig, self.signal_handler)
242248

243249
self.is_idle = True
244250

0 commit comments

Comments
 (0)