4

Consider this simple hello world flask example.

from flask import Flask

app = Flask(__name__)

@app.route("/inst")
def index():
    print('Hello World!')
    return "Hello World!"

if __name__ == '__main__':
    app.run(debug=True, use_reloader=False)

When I hit browser with the route url, the print statement gets executed twice. The output of console is as follows:

* Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [04/Jan/2020 18:22:46] "GET /inst HTTP/1.1" 200 -
Hello World!
Hello World!
127.0.0.1 - - [04/Jan/2020 18:22:46] "GET /inst HTTP/1.1" 200 -

Why does the GET /inst called twice? Is this way it's supposed to be?

Jagjot Singh
  • 61
  • 1
  • 6
  • Can you do the request using `curl` and see if the same thing happens? `curl -v localhost:5000/inst` – nima Jan 04 '20 at 13:06
  • With curl it runs only 1 time. This happens only with the browser. – Jagjot Singh Jan 04 '20 at 13:12
  • 1
    Then it's most probably an issue with your browser. Try another browser or use private/incognito mode on your browser. – nima Jan 04 '20 at 13:14
  • @n1rna What do you think one should look for in terms of the browser? I've actually experienced the exact same problem on the dev server and it's not at all clear to me where I should be looking to try and trace the issue – roganjosh Jan 04 '20 at 13:15
  • Isn't this due to the fact that you both print "Hello World!" and return "Hello World!"? The return is what is actually served over HTTP. – arghol Jan 04 '20 at 13:18
  • Honestly, I haven't seen such issue before. Maybe it is related to some proxy configuration or maybe something CORS related! – nima Jan 04 '20 at 13:19
  • @arghol granted, it would be better if they used two different strings between the two, but the return value should not be printed in the console. From my own experience, the double occurrence is just `print` being executed twice – roganjosh Jan 04 '20 at 13:19
  • Okay, it's happening only in chrome. I tested it on safari and it works fine. I'm wondering what could be the issue with chrome? – Jagjot Singh Jan 04 '20 at 13:22
  • 3
    maybe this is related to some extensions you have installed: https://stackoverflow.com/questions/4460661/what-to-do-with-chrome-sending-extra-requests – nima Jan 04 '20 at 13:24
  • @roganjosh changing the return string doesn't help. Same issue. – Jagjot Singh Jan 04 '20 at 13:24
  • @n1rna nice find. I'm also using chrome. So, the issue is not in Flask, it's the browser itself – roganjosh Jan 04 '20 at 13:26
  • @n1rna you nailed it. It was the extension. I uninstalled a couple of extensions and it works fine. I'm not sure but I think it was the AJAX debugger extension. – Jagjot Singh Jan 04 '20 at 13:29
  • @JagjotSingh cool ;) happy it helped you :) – nima Jan 04 '20 at 13:31
  • @n1rna Thanks. And thanks to all for participating and helping. – Jagjot Singh Jan 04 '20 at 13:38

1 Answers1

2

So the reason why my request was getting fired twice was because of some extension installed in chrome. I removed a couple of my extensions and it works fine. The credit for pointing out this goes to @n1rna. Thanks.

Jagjot Singh
  • 61
  • 1
  • 6
  • 1
    This might be my reason too. Because it happens on my computer and not for others. – Avi Sep 04 '20 at 19:49