在某些任务(如电子邮件发送)尚未完成之前,flask 如何渲染页面?
问题
在我们的例子中,当用户向服务器提交注册信息时,服务器会先发送一封认证邮件,然后呈现一个成功页面。但是邮件发送总是需要3~5秒,也就是说他提交注册信息后至少需要3秒才能得到回复。
我们所做的
1. 信号
我们尝试发送一个信号并触发一个邮件发送功能来做这件事:
if approved:
# ...
send_activation_signal.send(email)
login_user(u)
return redirect(url_for('register_success'))
结果
一切都很好,除了在 register_success 页面呈现之前至少需要 3 秒。
2. 分叉一个线程
分叉一个线程以完成电子邮件发送
t = threading.Thread(target = send_activation, args = (email, username, email_confirm_code, ))
t.start()
结果
但看起来请求上下文没有以正确的方式处理。
> Exception in thread Thread-2: Traceback (most recent call last):
> File
> "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
> line 552, in __bootstrap_inner
> self.run() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py",
> line 505, in run
> self.__target(*self.__args, **self.__kwargs) File "/Users/arthasshih/flask/happyvs/main_app/views/user.py", line 267, in
> send_activation
> app.logger.debug("sending #############") File "/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
> line 336, in __getattr__
> return getattr(self._get_current_object(), name) File "/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Werkzeug-0.8.3-py2.7.egg/werkzeug/local.py",
> line 295, in _get_current_object
> return self.__local() File "/Users/arthasshih/virtual_evns/normal/lib/python2.7/site-packages/Flask-0.8-py2.7.egg/flask/globals.py",
> line 19, in _lookup_object
> raise RuntimeError('working outside of request context') RuntimeError: working outside of request context