Django:连接数据库抛出自定义驱动程序

时间:2012-05-24 07:00:23

标签: django apache mongodb

我有使用PostgreSQL作为主要后端的Django项目:

settings.py

DATABASES = {
    'default': {
        # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3, 'oracle'.
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        ...
    },
}

但我在我的应用程序中也有与MongoDB的自定义连接:

mongo_connection.py

from pymongo import Connection
from pymongo.errors import ConnectionFailure
from django.db import connections

try:  
    connection = Connection(host="127.0.0.1", port=27017)
    db = connection[db_name]
    print "Connected successfully(Mongo, %s)" % (db_name,)
except ConnectionFailure, e:
    sys.stderr.write("Could not connect to MongoDB: %s" % e)
    sys.exit(1)

在生产服务器上,我注意到Apache的error.log中有一件事:

[Wed May 23 05:05:39 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 05:07:16 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 06:41:51 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 06:42:21 2012] [error] Connected successfully(Mongo, galio)
...
[Wed May 23 19:26:45 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 19:26:48 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 22:20:44 2012] [error] Connected successfully(Mongo, galio)
[Wed May 23 22:20:48 2012] [error] Connected successfully(Mongo, galio)
[Thu May 24 00:12:45 2012] [error] Connected successfully(Mongo, galio)

这意味着Apache偶尔会重新导入模块(我是对的吗?)。我有两个问题:

1)生产服务器的这种行为是否正常? 2)放置自定义连接的最佳位置在哪里?可能是我应该将此代码移至settings.py?

P.S。 Apache配置

/etc/apache2/sites-available/default

<VirtualHost *:80> 
    ServerAdmin it@asdf.as 
    ServerName ... 
    ServerAlias ... 
    DocumentRoot /var/www/ 
    LogLevel warn 
    WSGIDaemonProcess www-data processes=2 maximum-requests=500 threads=1
    WSGIProcessGroup www-data 
    WSGIScriptAlias / /home/disk/galio/galio/apache/django.wsgi 
    Alias /media /var/www/media/ 
</VirtualHost>

0 个答案:

没有答案