Search code examples
pythonmultithreadingflask-restful

Why am I instantiating two different queues?


I'm trying to set-up an application which will receive HTTP GET's and POST's using python and flask-restful. The problem that I'm getting is that when I start the application I see that there are two instances of a queue being generated. I would like you to help me understand why?

Application output (terminal):

<queue.Queue object at 0x10876fdd8>
 * Serving Flask app "main" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: on
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
<queue.Queue object at 0x10ce48be0>
 * Debugger is active!
 * Debugger PIN: 292-311-362

Python code (ran with the following command python -m main):

import json
from flask import Flask, request
from flask_restful import Resource, Api, reqparse
import requests
import os
import threading
import queue
import sys

app = Flask(__name__)
api = Api(app)

parser = reqparse.RequestParser()
parser.add_argument("endpoint")

queue = queue.Queue()

base_path = os.path.dirname(os.path.realpath(__file__))
config = Json_Parser().get_json_object(base_path + "path")

consumer = Consumer(config, queue)
t1 = threading.Thread(target=consumer.consume)
t1.start()


class Interaction(Resource):
    def get(self):
        self.create_interaction()
        thread_queue = consumer.get_queue()
        output = thread_queue.get()
        return output

api.add_resource(Interaction, '/interaction')

if __name__ == '__main__':
    print(queue)
    app.run(debug=True)

Solution

  • With the help of @Richar de Wit I changed the following line:

    app.run(debug=True)

    to:

    app.run(debug=True, use_reloader=False)

    to prevent the debugger to instantiate two queues thus giving issues later on.

    The problem is referenced in this question:

    Why does running the Flask dev server run itself twice?