summaryrefslogtreecommitdiff
path: root/examples/event_loops.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2017-03-06 12:01:11 -0500
committerGitHub <noreply@github.com>2017-03-06 12:01:11 -0500
commit27c182b14c6f2529fdb31b7b3dba1120ca23a3dc (patch)
treec8281430eb3a209a6506ac576d417f6e3f16dd48 /examples/event_loops.py
parent46f4f277d62e4b6e54fb525787474c34894a7481 (diff)
parent683ce1f875b68a19e096ce0970779adac76c3629 (diff)
downloadcmd2-git-27c182b14c6f2529fdb31b7b3dba1120ca23a3dc.tar.gz
Merge pull request #64 from python-cmd2/event_loops
Documented how to integrate cmd2 with event loops
Diffstat (limited to 'examples/event_loops.py')
-rwxr-xr-xexamples/event_loops.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/examples/event_loops.py b/examples/event_loops.py
new file mode 100755
index 00000000..e3e5880c
--- /dev/null
+++ b/examples/event_loops.py
@@ -0,0 +1,26 @@
+#!/usr/bin/env python
+# coding=utf-8
+"""A sample application for integrating cmd2 with external event loops.
+
+This is an example of how to use cmd2 in a way so that cmd2 doesn't own the inner event loop of your application.
+
+This opens up the possibility of registering cmd2 input with event loops, like asyncio, without occupying the main loop.
+"""
+import cmd2
+
+
+class Cmd2EventBased(cmd2.Cmd):
+ def __init__(self):
+ cmd2.Cmd.__init__(self)
+
+ # ... your class code here ...
+
+
+if __name__ == '__main__':
+ app = Cmd2EventBased()
+ app.preloop()
+
+ # Do this within whatever event loop mechanism you wish to run a single command
+ app.onecmd_plus_hooks("help history")
+
+ app.postloop()