在 Bottle 驱动的应用程序中,我应该在哪里存储每次请求时加载的数据?
我有一个简单的 Bottle 应用程序,它将其配置存储在文本文件中。当然,我需要该配置在应用程序的几乎每个处理程序中都可用,并且我希望在每个请求时重新加载它。
在 Flask 中,我可以将配置文件加载到 before_request
中的 ConfigParser
实例中,并将对其的引用放入 g
对象的属性中,只需正如 Flask 文档中所述,作为连接 SQLite 的一种方式。然后,g
对象将在每个处理程序中可用:
@app.before_request
def before_request():
g.config = load_config()
@app.route('/')
def index():
param = g.config.get(...)
...
另一方面,在 Bottle 中,线程局部变量被认为是一件坏事,存储数据库连接的建议方法是 编写一个插件,使用巧妙的技巧分析签名处理程序。虽然 SQLite 的问题已经解决,但对于其他任意的每个请求数据来说,问题还没有解决。
坦率地说,我不敢相信我应该编写 60 行代码来完成如此简单的任务,所以我想我一定错过了一些东西。
有什么想法吗?
I have a simple Bottle app which stores its configuration in a text file. Naturally, I need the config to be available in almost every handler of the app, and I want it to be reloaded on every request.
In Flask I could load the config file into a ConfigParser
instance in before_request
and put a reference to it into an attribute of the g
object, just as described in the Flask documentation as a way of connecting SQLite. The g
object would then be available in every handler:
@app.before_request
def before_request():
g.config = load_config()
@app.route('/')
def index():
param = g.config.get(...)
...
In Bottle, on the other hand, thread locals are considered a bad thing, and the suggested way of storing the DB connection is to write a plugin which analyzes the handlers for signatures using clever tricks. And while the problem is already solved for SQLite, it is not in case of some other arbitrary per-request data.
To be frank, I can't believe I'm supposed to write 60 lines of code to do such a simple task, so I guess I must be missing something.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然您希望它重新加载每个请求,为什么不只使用一个函数呢?
如果你想将其包装到插件中,这相当简单:
Since you want it reloaded every single request, why not just use a function?
If you wanted to wrap that into a plugin it's fairly simple: