mongoengine 0.4,django 1.3.1,uwsgi 1.0.2.1
在这一个人的头上碰到了我的头。我有一个使用mongoengine的Django应用程序。 mongodb服务器与我的应用服务器不同,某些事情(防火墙规则更改等)将导致连接丢失。
在这种情况下,我想实现处理自动重新连接,以便在发生这种情况时我的整个应用程序不会崩溃。我正在尝试这些方面:http://pastebin.com/HE8LSp5v(注意这不是我的代码,虽然我尝试修改它但没有成功)
该pastebin中的代码将成功检测到失败并尝试重新连接,但for循环通常不会多次迭代,因此您看到的是每秒重新连接尝试次数,每个uwsgi线程一次猜猜。
所以我稍微修改了代码(为https://gist.github.com/1723790)并将其扔到我的settings.py文件的底部。 uwsgi和dev服务器的结果是一样的,所以为了简单起见,我将在这里使用dev服务器。
启动开发服务器,我得到:
SETTING OVERRIDES
SETTING OVERRIDES DONE
WRAPPER ENTRY
WRAPPER ENTRY
SETTING OVERRIDES
SETTING OVERRIDES DONE
SETTING OVERRIDES
SETTING OVERRIDES DONE
WRAPPER ENTRY
WRAPPER ENTRY
SETTING OVERRIDES
SETTING OVERRIDES DONE
Validating models...
0 errors found
Django version 1.3.1, using settings 'mucs.settings'
Development server is running at http://0.0.0.0:80/
Quit the server with CONTROL-C.
随着mongoengine的运行,我访问了一个向它发出一些请求的页面。工作良好。然后关闭mongodb并刷新页面。我在屏幕上打印出这样的内容:
(REQUEST IS STARTED)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
WRAPPER ENTRY
PyMongo auto-reconnecting... could not connect to mucs-content:27017: [Errno 111] Connection refused. Waiting 0.5 seconds. (Attempt 0)
WRAPPER ENTRY
[...]
这样继续这样,直到我重新启动mongodb,此时页面才能正确呈现,并且工作正常。我不想把这些代码投入生产,因为我不确定这里到底发生了什么。看起来它可能会递归到循环中而不会拉回正确发生的AutoReconnect异常。我想接下来要做的就是检查每个操作的堆栈,看看到底发生了什么。