web.py:将初始化/全局变量传递给处理程序类?

发布于 2024-07-16 09:25:50 字数 225 浏览 2 评论 0原文

我正在尝试将 web.py 与 Tokyo Cabinet / pytc 一起使用,并且需要将数据库句柄(与 tokyo Cabinet 的连接)传递给我的处理程序类,以便它们可以与 tokyo Cabinet 进行通信。

有没有办法将处理程序传递给处理程序类的 init 函数? 或者我应该将句柄放入 globals() 中? 什么是 globals() 以及如何使用它?

I'm attempting to use web.py with Tokyo Cabinet / pytc and need to pass the db handle (the connection to tokyo cabinet) to my handler classes so they can talk to tokyo cabinet.

Is there a way to pass the handler to the handler class's init function? Or should I be putting the handle in globals() ? What is globals() and how do you use it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静水深流 2024-07-23 09:25:50

最好的方法是添加一个加载钩子(此处对 sqlalchemy 进行了描述)。 定义一个连接到 Tokyo Cabinet 的函数,并将生成的 db 对象作为 .orm 属性添加到 web.ctx,该属性在控制器内始终可用。

The best way would be to add a load hook (described here for sqlalchemy). Define a function that connects to Tokyo Cabinet and adds the resulting db object as an .orm attribute to web.ctx, which is always available inside the controller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文