0

I've created an API using Flask. I followed the factory pattern of Flask documentation - the rules and names will be abstracted. (__init__.py of my_package)

With the package and factory method done, I imported it into another file outside the package and created a small script to get the waitress server started (server.py file)

Everything works as expected, but I detected that every time I make a request to the API, it runs twice. This can't happen, as some routes insert data into my database and this behaviour will keep inputting duplicates.

How to properly solve this issue? Can I disable this double response?

I've already tried setting app.debug and app.use_reloader to False, without any noticeable difference.

I also tried to find some information in Waitress and Flask documentation but I could find anything that helped me.

Factory method of __init__.py inside my_package:

def create_app(test_config=None):
   app = Flask(__name__, instance_relative_config=True)
   app.config.from_object('config')

   @app.route('/route1', methods=['POST', 'PUT'])
   def route1():
      if request.method == 'POST':
        # route logic...
        pass
      elif request.method == 'PUT':
        # route logic...
        pass

   @app.route('/route2', methods=['POST'])
   def route2():
      if request.method == 'POST':
        # route logic...
        pass

   return app

server.py file, outside my_package:

import my_package
from waitress import serve

serve(my_package.create_app(), host='0.0.0.0', port=9600)

Expected: API requests will only run once per call.

Actual: API requests are being run twice per call.

1 Answers1

1

I recently had the same problem, the duplication only seems to happen when you specify host in serve. When you omit host, it is being run only once(this might only be the case for localhost, I haven't tested that).

Hope it helps

snowflake
  • 11
  • 1