为什么要实例化两个不同的队列?

时间:2019-05-14 11:25:25

标签: python multithreading flask-restful

我正在尝试设置一个使用python和flask-restful接收HTTP GET和POST的应用程序。我得到的问题是,当我启动应用程序时,我看到有两个队列实例正在生成。我希望您能帮助我理解为什么?

应用程序输出(终端):

<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代码(运行以下命令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)

1 个答案:

答案 0 :(得分:0)

在@Richar de Wit的帮助下,我更改了以下行:

app.run(debug=True)

收件人:

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

防止调试器实例化两个队列,从而在以后出现问题。

此问题引用了该问题:

Why does running the Flask dev server run itself twice?