Saturday, 28 January 2023

Running Twisted in background through layered python

What specific changes need to be made in the minimal reproducable code in this repository in order for the python app\appmain.py command to result in the Twisted server running the Flask API as a background process so that the control flow of the rest of the appmain.py program can continue?

The problem is clearly defined: A powershell command correctly runs Twisted as a background process, but then two layers of python subprocess successively invoke the correct powershell command as a foreground process, despite the fact that both layers of python subprocess invocations set shell=True to specify running as a background process.

The minimal code is very simple, and has been stripped down to the bare minimum required to reproduce the problem.

REPRODUCING THE ERROR:

Steps to reproduce the error:

  1. Make sure you have Python 3.10 installed on a windows machine
  2. git clone repo.git into windows machine
  3. Open up windows cmd and navigate to the root of the repo
  4. Run python app\appmain.py

The result will be that the twisted server is running but that the program execution is halted because Twisted is running in the foreground.

You can confirm the Twisted server is hosting the flask API by opening a web browser and navigating the browser to http://localhost:2345/hereis/the/endpoint/ where a simple {"hello":"world"} json will be output.

You can shut down the Twisted server by opening a second windows cmd window, navigating to the same directory, and typing python api\destroyIt.py so that you can resume testing from a clean slate.

THINGS THAT ARE CORRECT:

The base command run by the lowest level python in setItUp.py in the example works correctly and is:

powershell $a = start-process -NoNewWindow powershell { twistd web --wsgi myFlaskAPI.app } -PassThru
  

Next, the setItUp.py script includes shell=True in the subprocess.call(startTwistdCommand, shell=True) command that invokes what should be the correct powershell command.

Third, the api_caller.py class also includes shell=True in the subprocess.Popen( commandToRun,cwd=workingDir, stdout=subprocess.PIPE, shell=True) command that invokes setItUp.py

The shell=True argument in the python subprocess commands should cause the result to run in the background, according to our research.

PROBLEM RESTATEMENT:

So why does the Twisted server process halt the program by running Twisted in the foreground even though we seem to have made the correct changes in powershell and in python?

What specifically needs to be changed in the code given in order for the `` command to result in the Twisted server running in the background so that the appmain.py can continue running other commands?



from Running Twisted in background through layered python

No comments:

Post a Comment