我开发了一些类似 DAO 的自定义类来满足我的项目的一些非常专业的要求,该项目是一个不在任何类型框架内运行的服务器端进程.
I have developed some custom DAO-like classes to meet some very specialized requirements for my project that is a server-side process that does not run inside any kind of framework.
该解决方案非常有效,只是每次发出新请求时,我都会通过 MySQLdb.connect 打开一个新连接.
The solution works great except that every time a new request is made, I open a new connection via MySQLdb.connect.
将其切换到在 python 中使用连接池的最佳插入"解决方案是什么?我正在想象类似于 Java 的公共 DBCP 解决方案.
What is the best "drop in" solution to switch this over to using connection pooling in python? I am imagining something like the commons DBCP solution for Java.
该进程运行时间很长,并且有许多线程需要发出请求,但不是同时发出所有请求……具体而言,它们在短暂地写出一大块结果之前做了大量工作.
The process is long running and has many threads that need to make requests, but not all at the same time... specifically they do quite a lot of work before brief bursts of writing out a chunk of their results.
编辑添加:经过更多搜索,我发现 anitpool.py 看起来不错,但由于我对 python 比较陌生,我猜我只是想确保我不会错过更明显/更惯用/更好的解决方案.
Edited to add: After some more searching I found anitpool.py which looks decent, but as I'm relatively new to python I guess I just want to make sure I'm not missing a more obvious/more idiomatic/better solution.
IMO,更明显/更惯用/更好的解决方案"是使用现有的 ORM,而不是发明类似 DAO 的类.
IMO, the "more obvious/more idiomatic/better solution" is to use an existing ORM rather than invent DAO-like classes.
在我看来,ORM 比原始"更受欢迎;SQL 连接.为什么?因为 Python 是面向对象的,并且从 SQL 行到对象的映射绝对是必不可少的.处理不映射到 Python 对象的 SQL 行的用例并不多.
It appears to me that ORM's are more popular than "raw" SQL connections. Why? Because Python is OO, and the mapping from a SQL row to an object is absolutely essential. There aren't many use cases where you deal with SQL rows that don't map to Python objects.
我认为 SQLAlchemy 或 SQLObject(以及相关的连接池)是更惯用的 Pythonic 解决方案.
I think that SQLAlchemy or SQLObject (and the associated connection pooling) are the more idiomatic Pythonic solutions.
池作为一个单独的特性并不常见,因为纯 SQL(没有对象映射)对于从连接池中受益的复杂、长时间运行的进程并不是很流行.是的,使用纯 SQL ,但它总是用于更简单或更受控制的应用程序,其中池化没有帮助.
Pooling as a separate feature isn't very common because pure SQL (without object mapping) isn't very popular for the kind of complex, long-running processes that benefit from connection pooling. Yes, pure SQL is used, but it's always used in simpler or more controlled applications where pooling isn't helpful.
我认为您可能有两种选择:
I think you might have two alternatives:
这篇关于python中数据库连接池的最佳解决方案是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!