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

4
  • You can use the loops, yes, just not infinite ones Commented Oct 2, 2018 at 14:09
  • Can you try putting app = Flask(__name__) and @app.route("/") inside the def main():? Commented Oct 2, 2018 at 14:09
  • @Bazinga, then the Flask server won't start Commented Oct 2, 2018 at 14:11
  • Oh, ok. No experience with Flask so gave a naive suggestion Commented Oct 2, 2018 at 14:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

2 Comments

what is the main difference between multiprocessing and threading ?
This should answer that one.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.