| 1 | # Alternate Event Loops |
| 2 | |
| 3 | Throughout this documentation, we have focused on the 90% use case, which we believe applies to over |
| 4 | 90% of our user base. This focuses on ease of use and the best out-of-the-box experience, where |
| 5 | developers get the most functionality for the least amount of effort. We are talking about running |
| 6 | `cmd2` applications with the `cmdloop()` method: |
| 7 | |
| 8 | ```py |
| 9 | from cmd2 import Cmd |
| 10 | class App(Cmd): |
| 11 | # customized attributes and methods here |
| 12 | app = App() |
| 13 | app.cmdloop() |
| 14 | ``` |
| 15 | |
| 16 | However, there are some limitations to this way of using `cmd2`, mainly that `cmd2` owns the inner |
| 17 | loop of a program. This can be unnecessarily restrictive and can prevent using libraries which |
| 18 | depend on controlling their own event loop. |
| 19 | |
| 20 | Many Python concurrency libraries involve or require an event loop which they are in control of, |
| 21 | such as [asyncio](https://docs.python.org/3/library/asyncio.html), [gevent](http://www.gevent.org/), |
| 22 | [Twisted](https://twistedmatrix.com), etc. |
| 23 | |
| 24 | !!! warning |
| 25 | |
| 26 | As of version **4.0**, `cmd2` depends on `prompt-toolkit` which in turn uses |
| 27 | [asyncio natively](https://python-prompt-toolkit.readthedocs.io/en/stable/pages/advanced_topics/asyncio.html) |
| 28 | and starts its own `asyncio` event loop. |
| 29 | |
| 30 | The [async_call.py](https://github.com/python-cmd2/cmd2/blob/main/examples/async_call.py) example shows how |
| 31 | to make a call to an async function from a cmd2 command. |
| 32 | |
| 33 | `cmd2` applications can be executed in a way where `cmd2` doesn't own the main loop for the program |
| 34 | by using code like the following: |
| 35 | |
| 36 | ```py |
| 37 | import cmd2 |
| 38 | |
| 39 | class Cmd2EventBased(cmd2.Cmd): |
| 40 | def __init__(self): |
| 41 | cmd2.Cmd.__init__(self) |
| 42 | |
| 43 | # ... your class code here ... |
| 44 | |
| 45 | if __name__ == '__main__': |
| 46 | app = Cmd2EventBased() |
| 47 | app.preloop() |
| 48 | |
| 49 | # Do this within whatever event loop mechanism you wish to run a single command |
| 50 | cmd_line_text = "help history" |
| 51 | app.runcmds_plus_hooks([cmd_line_text]) |
| 52 | |
| 53 | app.postloop() |
| 54 | ``` |
| 55 | |
| 56 | The [cmd2.Cmd.runcmds_plus_hooks][] method runs multiple commands where each single command is |
| 57 | executed via [cmd2.Cmd.onecmd_plus_hooks][]. |
| 58 | |
| 59 | The [cmd2.Cmd.onecmd_plus_hooks][] method will do the following to execute a single command in a |
| 60 | normal fashion: |
| 61 | |
| 62 | 1. Parse user input into a [cmd2.Statement][] object |
| 63 | 1. Call methods registered with [cmd2.Cmd.register_postparsing_hook][] |
| 64 | 1. Redirect output, if user asked for it and it's allowed |
| 65 | 1. Start timer |
| 66 | 1. Call methods registered with [cmd2.Cmd.register_precmd_hook][] |
| 67 | 1. Call [cmd2.Cmd.precmd][] - for backwards compatibility with `cmd` |
| 68 | 1. Add statement to [History](../features/history.md) |
| 69 | 1. Call `do_command` method |
| 70 | 1. Call methods registered with [cmd2.Cmd.register_postcmd_hook][] |
| 71 | 1. Call [cmd2.Cmd.postcmd][] - for backwards compatibility with `cmd` |
| 72 | 1. Stop timer and display the elapsed time |
| 73 | 1. Stop redirecting output if it was redirected |
| 74 | 1. Call methods registered with [cmd2.Cmd.register_cmdfinalization_hook][] |
| 75 | |
| 76 | Running in this way enables the ability to integrate with an external event loop. However, how to |
| 77 | integrate with any specific event loop is beyond the scope of this documentation. Please note that |
| 78 | running in this fashion comes with several disadvantages, including: |
| 79 | |
| 80 | - Requires the developer to write more code |
| 81 | - Does not allow commands at invocation via command-line arguments |