How can I create a command line interface that persists as a background process and only executes commands when specific commands are entered? The following is pseudo code:
def startup():
# costly startup that loads objects into memory once in a background process
def tear_down()
# shut down; generally not needed, as main is acting like a service
def main():
startup()
# pseudo code that checks shell input for a match; after execution releases back to shell
while True:
usr_in = get_current_bash_command()
if usr_in == "option a":
# blocking action that release control back to shell when complete
if usr_in == "option b":
# something else with handling like option a
if usr_in == "quit":
# shuts down background process; will not be used frequently
tear_down()
break
print("service has been shut down. Bye!")
if __name__ == "__main__":
# run non-blocking in background, only executing code if usr_in matches commands:
main()
Note what this is not:
- typical example of argparse or click, that runs a (blocking) python process until all commands are completed
- a series of one-off scripts for each command; they utilize objects in memory instantiated in the background process with startup()
- a completely different shell, like ipython; I'd like to integrate this with a standard shell, e.g. bash.
I am familiar with click, argparse, subprocess, etc., and as far as I can tell they accept a single command and block until completion. I specifically seek something that interacts with a background process so that expensive startup (loading objects into memory) is handled only once. I have looked at both python daemon and service packages, but I'm unsure if these are the right tool for the job also.
How can I accomplish this? I feel as though I don't know what to google to get the answer I need...
zeromqwhich works really well but has a bit of a learning curve. But when I throw something together quick and dirty XMLRPC is a good choice. Just IMHO.