aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/examples/console.py27
1 files changed, 23 insertions, 4 deletions
diff --git a/python/examples/console.py b/python/examples/console.py
index aa6fff3..1694ba5 100644
--- a/python/examples/console.py
+++ b/python/examples/console.py
@@ -166,6 +166,12 @@ def _here_now_command_handler(channel):
pubnub.here_now(channel, callback=_callback, error=_error)
+def kill_all_threads():
+ for thread in threading.enumerate():
+ if thread.isAlive():
+ thread._Thread__stop()
+
+
class DevConsole(Cmd):
@@ -177,11 +183,13 @@ class DevConsole(Cmd):
self.default_channel = None
pubnub = Pubnub("demo", "demo")
- def cmdloop(self):
+ def cmdloop_with_keyboard_interrupt(self):
try:
- Cmd.cmdloop(self)
- except KeyboardInterrupt as e:
self.cmdloop()
+ except KeyboardInterrupt:
+ print 'KB KeyboardInterrupt'
+ sys.stdout.write('\n')
+ kill_all_threads()
@options([make_option('-p', '--publish-key', action="store", default="demo", help="Publish Key"),
@@ -331,9 +339,20 @@ class DevConsole(Cmd):
_subscribe_command_handler(opts.channel)
+ def do_exit(self, args):
+ kill_all_threads()
+ return -1
+
+ def do_EOF(self, args):
+ kill_all_threads()
+ return self.do_exit(args)
+
+ def handler(self, signum, frame):
+ kill_all_threads()
+
def main():
app = DevConsole()
- app.cmdloop()
+ app.cmdloop_with_keyboard_interrupt()
if __name__ == "__main__":
main()