我希望在django支持的Web应用程序中保持对第三方遗留数据库的持久连接.
我想保持Web应用程序和旧数据库之间的连接打开,因为为这个特殊的数据库创建新连接非常慢.
它不像通常的连接池,因为我需要存储每个Web用户的连接.用户"Foo"需要在Web服务器和旧版DB之间建立自己的连接.
到目前为止,我使用Apache和wsgi,但如果其他解决方案更合适,我可以改变.
到目前为止我使用django.在这里我也可以改变.但是痛苦会更大,因为已经有很多代码需要再次集成.
到目前为止,我使用Python.我想Node.js在这里会更合适,但改变的痛苦太高了.
当然需要某种超时.如果N分钟没有来自用户"Foo"的http请求,那么持久连接将需要关闭.
怎么能解决这个问题?
更新
我叫它,DB
但它不是通过settings.DATABASES配置的数据库.这是一个奇怪的,传统的,不是广泛的类似DB的系统,我需要集成.
如果我现在有50个人在线使用网络应用程序,那么我需要有50个持久连接.每个用户一个.
用于连接到DB的代码
我可以在每个请求中执行此行:
strangedb_connection = strangedb.connect(request.user.username)
但是这个操作很慢.使用连接很快.
当然strangedb_connection
不能序列化,也不能存储在会话中:-)
Your picture currently looks like:
user -----------> webserver <--------[1]--> 3rd party DB connection [1] is expensive.
You could solve this with:
user ----> webserver <---> task queue[1] <---> worker daemon <--[2]-> 3rd party DB [1] task queue can be redis, celery or rabbitmq. [2] worker daemon keeps connection open.
A worker daemon would do the connection to the 3rd party database and keep the connection open. This would mean that each request would not have to pay the connection costs. The task queue would be the inter-process communication, dispatching work to the daemon and do the queries in the 3rd party db. The webserver should be as light as possible in terms of processing and let workers do expensive tasks.
preloading with apache + modwsgiYou can actually preload
and have the expensive connection done before the first request. This is done with the WSGIImportScript
configuration directive. I don't remember at the top of my head if having a pre-load + forking configuration means each request will already have the connection opened and share it; but since you have most of the code, this could be an easy experiment.
uwsgi
supports preloading too. This is done with the import
directive.