带 Pyramid 的独立 web2py-DAL - 请求、线程和内存

发布于 2024-10-30 05:14:31 字数 482 浏览 5 评论 0原文

我正在 Pyramid 框架上构建一个应用程序,并希望使用 web2py-DAL 。 Firebird 嵌入式数据库是首选数据库。

在我尝试从一个网页异步调用多个视图之前,这非常有效。 kinterbasdb 驱动程序会出现不同的错误,例如“无效的游标状态”、“无效的游标引用”或“尝试重新关闭已关闭的游标”,其中 sqlite 会崩溃,没有任何错误消息,并会使用 python。这些视图可调用函数除了通过 SELECTing 进行简单读取之外什么也不做。

当金字塔根工厂为每个请求返回相同的 DAL 对象时,就会发生这种情况。似乎形成不同请求的线程正在使用相同的游标对象,因此游标被关闭,而另一个线程假设游标位于此处。

如果我在每个请求上创建一个新的 DAL 对象,则会遇到另一个问题 - 每个请求上的每个新连接都会分配内存,并且该内存不会被释放。因此,在一些讲解员请求后,有数百 MB 的内存被浪费。

不幸的是 Sqlalchemy 不适合这个项目。

有什么想法吗?

I'm building an app on Pyramid framework and would like to use the web2py-DAL with it. Firebird-embedded is the database of choice.

That works great until I try to call multiple views from one web page asynchronously. Different errors like "Invalid cursor state", "Invalid cursor reference" or "Attempt to reclose a closed cursor" are coming up from the kinterbasdb driver where sqlite just breaks down without any error messages and takes python with it. These view callables are doing nothing but simple reads by SELECTing.

This happens in the case that pyramids root factory returns the same DAL object with each request. It seems like threads form different requests are working with the same cursor object, so the cursor gets closed while the other thread assumes the cursor to be here.

If I create a new DAL object on each request I get an other problem - every new connection on each request allocates memory and this memory doesn't get free. So after some docents requests there are some hundred MB wasted memory.

Unfortunately Sqlalchemy is not an option for this project.

Are there any ideas?

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

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

发布评论

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

评论(2

瞎闹 2024-11-06 05:14:31

要点是 DAL 对象应该在每个请求上创建。但之后必须手动关闭。

我是这样做的:

request.add_finished_callback

请求对象有一个属性,因此我以这种方式扩展了 DAL 对象:

class Root(DAL):
    def __init__(self, request, uri):
        DAL.__init__(self, uri, pool_size=0)
        request.add_finished_callback(self._close)

    def _close(self, request):
        self._adapter.close_all_instances('commit')

然后 root_factory 在每个请求上返回一个新的根对象。

感谢 web2py-users 群组!

The point was that the DAL object should be created on every request. But after that it must be closed manually.

I did it in that way: There is a

request.add_finished_callback

property of the request object, so I extended the DAL object in that way:

class Root(DAL):
    def __init__(self, request, uri):
        DAL.__init__(self, uri, pool_size=0)
        request.add_finished_callback(self._close)

    def _close(self, request):
        self._adapter.close_all_instances('commit')

A new Root-object is then returned by the root_factory on each request.

Thanks to the web2py-users group!

土豪我们做朋友吧 2024-11-06 05:14:31

web2py 邮件列表中的一些解决方案

Some solutions from the web2py mailing list.

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