0

我有使用 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 吗?

PS 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>
4

0 回答 0