1

I try to create a Web Server using Flask and I want to add an infinite loop just like this:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def main():
   return "This is my website". 

if __name__ == "__main__":
   app.run()
   while True:
       print("Hello World")

but when I run the program, string "Hello World" is never printed, is there a solution for this ? Thanks

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245

1 Answers1

1

That is to be expected, since app.run() is an infinite loop via werkzeug(which is a WSGI utility library that is used by flask). So the simple answer is no, it is not possible under those circumstances. What you'll need to do is use something like multiprocessing or threading

Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26