假设我的函数“search()”获取mongodb中的一些内容并返回生成器。
我的烧瓶视图功能如下所示
@app.route("search/")
def search_page():
generator = search()
return Response(generator)
但如果我这样做,我会收到此错误:
Error on request:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 180, in run_wsgi
execute(self.server.app)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 171, in execute
write(data)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 151, in write
assert type(data) is bytes, 'applications must write bytes'
AssertionError: applications must write bytes
生成器本身会产生几个json值。我总是可以使用生成器来构造一个列表并返回列表,但我想避免这样做。