使用 webpy 线程化特定数据

发布于 2024-07-11 22:54:01 字数 219 浏览 5 评论 0原文

我正在用 webpy 编写一个小网络应用程序,我想知道是否有人有关于我遇到的小问题的任何信息。

我已经编写了一个小型 ORM 系统,它似乎运行得很好。 理想情况下,我想用 webpy 将它缝合起来,但似乎仅按原样使用它会导致线程问题(数据库连接是跨线程边界实例化/访问的,或者异常状态)。

有谁知道我如何(在 webpy 中)在与页面处理代码的其余部分相同的线程上创建数据库连接?

I'm writing a little web app with webpy, and I'm wondering if anyone has any information on a little problem I'm having.

I've written a little ORM system, and it seems to be working pretty well. Ideally I'd like to stitch it in with webpy, but it appears that just using it as is causes thread issues (DB connection is instantiated/accessed across thread boundaries, or so the exception states).

Does anyone know how I can (within the webpy) create my DB connection on the same thread as the rest of the page handling code will be?

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

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

发布评论

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

评论(2

强辩 2024-07-18 22:54:01

我们将 SQLAlchemy 与 web.py 结合使用,并使用钩子来根据请求创建和关闭数据库连接。 SQLAlchemy 处理池化,因此并非每个连接都是 tcp 连接。

您要使用的线程本地存储是 web.ctx 即。 任何时候访问 web.ctx 时,您只能看到该线程设置的属性。

我们的代码如下所示:

def sa_load_hook():
    web.ctx.sadb = Session()

def sa_unload_hook():
    web.ctx.sadb.close()

web.loadhooks['sasession'] = sa_load_hook
web.unloadhooks['sasession'] = sa_unload_hook

将 Session 替换为您的数据库连接函数,它应该可以正常工作。

We use SQLAlchemy with web.py and use hooks to create and close db connections per request. SQLAlchemy handles pooling, so not every connection is a tcp connection.

The thread local storage you want to use is web.ctx ie. any time you access web.ctx you only see properties set by that thread.

Our code looks something like this:

def sa_load_hook():
    web.ctx.sadb = Session()

def sa_unload_hook():
    web.ctx.sadb.close()

web.loadhooks['sasession'] = sa_load_hook
web.unloadhooks['sasession'] = sa_unload_hook

Replace Session with your db connection function and it should work fine for you.

赠佳期 2024-07-18 22:54:01

我会尝试一下这个。 免责声明:我没有使用 web.py 框架的经验。

我建议您尝试以下操作:

(1)创建一个全局 threading.local 实例来跟踪您的线程本地对象(在您的情况下,它将仅跟踪一个对象,即数据库会话)。

import threading
serving = threading.local()

(2) 在每个请求开始时,创建一个数据库连接/会话并将其保存在 threading.local 实例中。 如果我正确理解 web.py 文档,您可以执行以下操作:

def setup_dbconnection(handler): 
    serving.dbconnection = create_dbconnection(...)
    try:
        return handler()
    finally:
        serving.dbconnection.close() # or similar

app.add_processor(setup_dbconnection)

(3) 在您的控制器中方法(如果它们在 web.py 中被称为?),每当您需要数据库连接时,请使用serving.dbconnection。

I'll give this one a try. Disclaimer: I have no experience with the web.py framework.

I suggest you try the following:

(1) Create a global threading.local instance to keep track of your thread local objects (in your case, it will keep track of only one object, a database session).

import threading
serving = threading.local()

(2) At the start of each request, create a db connection/session and save it in the threading.local instance. If I understand the web.py documentation correctly, you can do the following:

def setup_dbconnection(handler): 
    serving.dbconnection = create_dbconnection(...)
    try:
        return handler()
    finally:
        serving.dbconnection.close() # or similar

app.add_processor(setup_dbconnection)

(3) In your controller methods (if they're called that in web.py?), whenever you need a db connection, use serving.dbconnection.

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