Flask restful GET 在应用内没有响应

Flask restful GET doesn't respond within app

我有一个带端点的烧瓶restfulapi

api.add_resource(TestGet, '/api/1/test')

并且我想使用来自该端点的数据来填充我的神社模板。但是每次我尝试在这样的示例路由中调用它时

@app.route('/mytest') def mytest(): t = get('http://localhost:5000/api/1/test') 它从不 returns 任何东西并保持在一个循环中,这意味着它正在处理请求而不是 returns。我无法在同一个烧瓶应用程序中调用它有什么原因吗?我能够通过浏览器和另一个 python REPL 到达端点。完全困惑为什么会发生这种情况以及为什么它从来没有 returns 任何事情。至少期待一个错误。

这是我正在尝试的全部示例运行

from flask import Flask
from requests import get

app = Flask('test')

from flask_restful import Api, Resource

api = Api(app)

class TestGet(Resource):
    def get(self):
        return {'test': 'message'}

api.add_resource(TestGet, '/test')

@app.route('/something')
def something():
    resp = get('http://localhost:5000//test').json
    print(resp)
from gevent.wsgi import WSGIServer
WSGIServer(('', 5000), app).serve_forever()

请参阅此 SO 线程,其中对 Flask 限制进行了很​​好的解释: 具体来说,在您的情况下,您正在点击这个:

The main issue you would probably run into is that the server is single-threaded. This means that it will handle each request one at a time, serially. This means that if you are trying to serve more than one request (including favicons, static items like images, CSS and Javascript files, etc.) the requests will take longer. If any given requests happens to take a long time (say, 20 seconds) then your entire application is unresponsive for that time (20 seconds).

因此,在请求中发出请求会使您的应用程序陷入僵局。

如果您只想调试程序,请使用 app.run(threaded=True)。这将为每个请求启动一个新线程。