我有一个从10秒开始的过程,但是在10秒之后,我不需要将信息返回给用户而是把它写在一个dynamoDB上,这就是为什么我希望用户不必等待10 秒。相反,我希望在发布请求后立即获得“成功”响应。
我读了几篇文章并在this第一,答案是使用 Teardown Callback,但没有示例。
然后我读了this ,但这对我的问题没有帮助。
我当然读了teardown-callbacks和 this pattern但我不知道如何以其他方式使用它。
我的代码是这样的:
@app.route('/ocr/read_image', methods=['POST'])
def get_text():
return jsonify('Success')
@app.teardown_request
def teardown_request(response):
time.sleep(10)
它实际上返回了“成功”消息,但就在 10 秒之后。
有没有办法在 10 秒之前返回“成功”消息?
我一直在读到 celery 可能是可行的,但如果可以的话,我很想避免使用它。
有人知道怎么做吗?
最佳答案
线程在这里也不必要地使事情复杂化。要在不引入线程的情况下做您想做的事情,您可以 Hook WSGI response close method .最简单的方法是使用 werkzeug ClosingIterator helper 。
import traceback
from werkzeug.wsgi import ClosingIterator
class AfterThisResponse:
def __init__(self, app=None):
self.callbacks = []
if app:
self.init_app(app)
def __call__(self, callback):
self.callbacks.append(callback)
return callback
def init_app(self, app):
# install extensioe
app.after_this_response = self
# install middleware
app.wsgi_app = AfterThisResponseMiddleware(app.wsgi_app, self)
def flush(self):
try:
for fn in self.callbacks:
try:
fn()
except Exception:
traceback.print_exc()
finally:
self.callbacks = []
class AfterThisResponseMiddleware:
def __init__(self, application, after_this_response_ext):
self.application = application
self.after_this_response_ext = after_this_response_ext
def __call__(self, environ, start_response):
iterator = self.application(environ, start_response)
try:
return ClosingIterator(iterator, [self.after_this_response_ext.flush])
except Exception:
traceback.print_exc()
return iterator
然后您可以像这样使用这个扩展:
import flask
import time
app = flask.Flask("after_response")
AfterThisResponse(app)
@app.route("/")
def home():
@app.after_this_response
def post_process():
time.sleep(2)
print("after_response")
return "Success!\n"
当您 curl 时,您会立即看到成功,然后 2 秒后您会在您的日志中看到您的“after_response”消息:
127.0.0.1 - - [25/Jun/2018 16:15:01] "GET / HTTP/1.1" 200 -
after_response
这个解决方案是根据我的答案改编的摘要:
关于python - 在 Flask 中返回立即响应,但在新线程中完成处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47942514/