gpt4 book ai didi

python-3.x - 在 flask 中发送发布请求时出错(python)

转载 作者:行者123 更新时间:2023-12-03 08:14:02 24 4
gpt4 key购买 nike

我正在向协程函数发送一个发布请求,但我遇到了错误。

    Traceback (most recent call last):
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1994, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1985, in wsgi_app
response = self.handle_exception(e)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1540, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/usr/local/lib/python3.4/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/usr/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/usr/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/usr/local/lib/python3.4/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'generator' object is not callable

以下是提交按钮后调用的函数:
并且函数 f() 正在从 get 请求中获取一些数据并从响应中返回 json。
    @app.route('/submit', methods=['POST'])
@asyncio.coroutine
def submit():
result = yield from f()
return render_template('test.html', result=result)


def f()
response = yield from request('get',...)
....

最佳答案

您不能将协程用作 Flask 路由,这不是 Flask 或 WSGI 协议(protocol)的工作方式。 WSGI 为每个线程/进程/eventlet 处理一个请求/响应。如果您需要执行后台任务,请生成一个线程或使用 Celery 之类的任务队列。

您可以将包含生成器的响应返回到 stream the response .它仍然会在一个线程中同步运行。

def stream():
yield 'thing one'
yield from other_thing()
yield 'thing two'

return current_app.response_class(stream())

如果你使用gevent,你可以 use eventlets instead of threads处理每个请求,在这种情况下,您可以生成其他小事件。这带来了它自己的性能问题,因为所有小事件都在一个线程中循环运行(asyncio 也是如此)。 Gunicorn 也支持 aiohttp,但 Flask 不支持,尽管它可能会运行。

关于python-3.x - 在 flask 中发送发布请求时出错(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43147893/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com