0

I am an absolute beginner to use flask

I am trying to run this code to generate an api that return hello world text I run it on jupyter notebook

from flask import Flask
app = Flask(__name__)

@app.route('/result')
def index():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug= False , host= 'localhost',port=2000)

The problem is when I run on any port number it runs fine the first time I tried to stop the cell using ctrl + c but it doesn't stop so I stopped it using interput kernel button but when I run the code again on the same port number it does not work any more and give me loading page forever but when I change the port number it works fine again can some explain why does it happen

Eyad Essa
  • 1
  • 3
  • Check this answer. You could implement what is there to stop the flask app by code. You can then put this code at the beginnig of the cell with some checks, so if the app exists, stop it before running the cell again. https://stackoverflow.com/questions/15562446/how-to-stop-flask-application-without-using-ctrl-c – Sembei Norimaki May 03 '23 at 12:53

2 Answers2

0

There is a good chance that when you switch to another instance of Flask, the previous instance is still alive.

As a result you cant start Flask on port X.
However when you switch to port Y - you are able to create another copy of your app.

Do.

ps -ef grep python

and you will see all your Flask instances

balderman
  • 22,927
  • 7
  • 34
  • 52
0

I have found a way to kill the port in windows 10 to be able to use it again

I use these commands in CMD run as admin

netstat -a -n -o | find "<your port id>"

this will display all details about this port and the PID of it

then I kill this pid using this command

taskkill /PID <your pid number>/F
Eyad Essa
  • 1
  • 3