python - 从 python 烧瓶中的另一个文件调用 send_file 函数时出错 | RuntimeError:在请求上下文之外工作
问题描述
问候堆栈溢出社区,我目前正在开发一个烧瓶应用程序,我正在尝试使用烧瓶中的 send_file 方法从辅助函数中检索文件。
我有一条这样的路线:
@app.route("/process",methods=['GET','POST'])
def do_something():
process = threading.Thread(target=function_name,args=[arg1,arg2])
process.start()
return render_template("template.html")
(function_name
在不同的文件上)函数应该像这样返回一个文件
def function_name():
filename = 'ohhey.pdf'
return send_file(filename,as_attachment=True,cache_timeout=0)
当我像这样运行我的应用程序时,出现以下错误
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
所以我尝试更改以下功能:
def function_name():
filename = 'ohhey.pdf'
with app.app_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
并得到这个新错误
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
所以我尝试以下方法:
def function_name():
filename = 'ohhey.pdf'
with app.test_request_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
进行此最终更改后,我的应用程序不会返回文件或错误。我感谢您的帮助。
解决方案
推荐阅读
- java - JavaFX - 沿着平行于切线的路径制作动画
- python - python 一组所有数字类型。do pop(),不随机输出
- docker - 是否可以“重置” Docker 容器的读/写层?
- python - 如何在python中将None分配给对象引用
- python - python - 区分图例中的 x 和 y 误差
- node.js - MongoDB insertMany BulkWriteError,避免捕获重复键并执行 then 功能
- python - 抛出异常:读取访问冲突。**bp** 为 0xFFFFFFFFFFFFFFFF
- jpa - JPA 实体管理器是线程本地的吗?
- javascript - 只有元组时如何将数据从 JSON 加载到 D3 图表
- ms-project - MS Project:如何使用 JavaScript 插件设置任务的日常实际工作?