From 16ec61f4d657f23d1e2361f86aeece31c74b84c3 Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Sun, 2 Mar 2008 15:50:05 +0000 Subject: [PATCH] Thread exiting elegance. --- __init__.py | 12 +++++++++--- irc.py | 4 +++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/__init__.py b/__init__.py index 752fd02..16309f0 100755 --- a/__init__.py +++ b/__init__.py @@ -38,8 +38,12 @@ def run_phenny(config): Watcher() while True: - connect(config) - if not isinstance(delay, int): break + try: connect(config) + except KeyboardInterrupt: + sys.exit() + + if not isinstance(delay, int): + break warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay print >> sys.stderr, warning @@ -47,7 +51,9 @@ def run_phenny(config): def run(config): t = threading.Thread(target=run_phenny, args=(config,)) - t.start() + if hasattr(t, 'run'): + t.run() + else: t.start() if __name__ == '__main__': print __doc__ diff --git a/irc.py b/irc.py index 89114af..38fe886 100755 --- a/irc.py +++ b/irc.py @@ -65,7 +65,9 @@ class Bot(asynchat.async_chat): print >> sys.stderr, message, self.create_socket(socket.AF_INET, socket.SOCK_STREAM) self.connect((host, port)) - asyncore.loop() + try: asyncore.loop() + except KeyboardInterrupt: + sys.exit() def handle_connect(self): if self.verbose: