'child process causes express server to restart when selenium script is done executing

I have an express server that runs a python script to grab some data. The selenium script does everything successfully, but the second it gets done executing, my express server restarts instead of continuing on to the next instructions.

express server:

const express = require("express")
const app = express()
const spawn = require("child_process").spawn

app.get("/", (req, res) => {
  console.log("before")
  const pythonProcess = spawn("python", ["./main.py"])
  pythonProcess.stdout.on("data", data => {
    console.log("done")
  })
console.log('done')
})

app.listen(6000, console.log("server started on 6000"))

python:

import sys

# driver does some stuff

print('first')
driver.close()
time.sleep(5)
print('second')
sys.stdout.flush('test')

"first" and "second" both get printed, but "test" does not.

This is what I get in the console:

[nodemon] restarting due to changes...
[nodemon] starting `node app.js`


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source