在WSGI中发送响应而不返回
有没有办法把一个WSGI应用的方法包裹起来,让服务器在调用特定方法时就发送响应,而不是等到这个应用的方法返回一个结果的时候再发送?
简单来说,我想要一个类似于finish_response(responseValue)
的方法。
1 个回答
3
WSGI 应用程序必须返回一个可迭代的对象,无论如何。如果你想发送部分响应,可以把你的应用程序改成一个生成器(或者返回一个迭代器):
import wsgiref, wsgiref.simple_server, time
def app(environ, start):
start('200 OK', [('Content-Type', 'text/plain')])
yield 'hello '
time.sleep(10)
yield 'world'
httpd = wsgiref.simple_server.make_server('localhost', 8999, app)
httpd.serve_forever()