title: command line interface for background python process
body: <p>How can I create a command line interface that persists as a background process and only executes commands when specific commands are entered? Please note the following is basically pseudo code:</p>
  
  <pre class="lang-py prettyprint-override"><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()
  </code></pre>
  
  <p>Note what this is not:</p>
  
  <ul>
  <li>typical example of argparse or click, that runs a (blocking) python process until all commands are completed</li>
  <li>a series of one-off scripts for each command; they utilize objects in memory instantiated in the background process with startup()</li>
  <li>a completely different shell, like ipython; I'd like to integrate this with a standard shell, e.g. bash.</li>
  </ul>
  
  <p>I am familiar with <a href="https://www.google.com/search?client=safari&amp;rls=en&amp;q=python%20click&amp;ie=UTF-8&amp;oe=UTF-8" rel="nofollow noreferrer">click</a>, <a href="https://www.google.com/search?client=safari&amp;rls=en&amp;q=argparse&amp;ie=UTF-8&amp;oe=UTF-8" rel="nofollow noreferrer">argparse</a>, <a href="https://docs.python.org/3/library/subprocess.html" rel="nofollow noreferrer">subprocess</a>, 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 <a href="https://pypi.org/project/python-daemon/" rel="nofollow noreferrer">daemon</a> and <a href="https://python-service.readthedocs.io/en/stable/" rel="nofollow noreferrer">service</a> packages, but I'm unsure if these are the right tool for the job also.</p>
  
  <p>How can I accomplish this?  I feel as though I don't know what to google to get the answer I need...</p>