I have successfully started a node server script through a subprocess call and capture the output in python:
subprocess.check_output(["node", "path/to/script"])
Now, because python is synchronous it doesn't run any code after that line above as it's waiting for the server to 'finish'. I need to run the node script by using that command and then immediately allow all code after that line but have the ability to capture every output from the server.
Is this possible?
EDIT:
After MarsNebulaSoup's answer with using asyncio, no code is ran until the nodejs server is stopped:
async def setupServer():
output = subprocess.run(["node", '/path/to/app.js'])
print('continuing')
async def setupController():
print('Running other code...')
async def mainAsync():
await asyncio.gather(setupServer(), setupController())
asyncio.run(mainAsync())
print('THIS WILL RUN ONCE THE SEVER HAS SETUP HAS STOPPED')
It will come as this order:
- 'Output of the subprocess command'
- ONLY AFTER THE SERVER HAS STOPPED: 'continuing'
- 'Running other code...'
- 'THIS WILL RUN ONCE THE SEVER HAS SETUP HAS STOPPED'