Gunicorn 不允许在同一服务器实例上请求 url

Gunicorn doesn't allow requesting urls on the same server instance

我遇到了一个奇怪的问题。我有以下 simple/sample Flask 应用程序:

# from __future__ import print_function
import requests

from flask import Flask

PORT = 6556
app = Flask(__name__)


@app.route('/')
def index():
    j = {'data': 1}

    print('INDEX CALLING API')
    response = requests.post('http://localhost:{}/fake_api'.format(PORT), json=j)
    print('INDEX CALLED API')

    return response.text

@app.route('/fake_api', methods=['POST'])
def fake_api():
    print('FAKE_API CALL RECEIVED')
    return 'Ok, it works'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=PORT, debug=True)

基本上,我正在模拟的是调用 url (http://localhost:6556/) 并使用 requests 另一个 url 同一个 flask 应用程序进行路由调用( http://localhost:6556/fake_api)。非常平庸,如果我只是用 python server.py.

启动应用程序,它就可以正常工作

问题是,当我尝试 运行 像这样使用 gunicorn 的服务器时

gunicorn -b 0.0.0.0:6556 server:app

应用程序在收到请求后无限期挂起。事实上它打印 'INDEX CALLING API' 然后停止。

我在使用 uWSGI 时遇到了同样的问题。有什么想法吗?

编辑

我接受 ikkuh 的回答,因为这个问题似乎与可通过生成多个进程解决的死锁有关。但是,对我有用的是安装 gevent 并将其设置为工作管理器,因为默认值为 sync:

# pip install gevent
gunicorn -b 0.0.0.0:6556 -k gevent server:app

当 运行 使用 gunicorn 时似乎会造成死锁。当 运行 有多个 worker 时似乎 运行 没问题,如下所示:

gunicorn -b 0.0.0.0:6556 --workers=2 server_test:app

这显然不是真正的解决方案,因为多个并发请求仍然会造成死锁。我不知道为什么 运行 没有 gunicorn 时不会发生这种情况。