Django pre-shutdown hook to close hanging pymongo

2019-04-02 14:51发布

I'm using pymongo in a Django project, and recently I've began to run into a problem where, upon exiting the main Django process (even through a management command) the pymongo connection will hang, and the process will never exit. Obviously, there's something wrong somewhere in the stack, but for now the best solution seems to be to explicitly close the connection before Django exits.

So: is there a pre-shutdown signal or hook that Django provides for this?

BTW: my connection code in case you're interested.

from django.conf import settings
from pymongo import ReplicaSetConnection, ReadPreference

conn = ReplicaSetConnection(
    hosts_or_uri=settings.MONGO['HOST'],
    replicaSet=settings.MONGO['REPLICASET'],
    safe=settings.MONGO.get('SAFE', False),
    journal=settings.MONGO.get('JOURNAL', False),
    read_preference=ReadPreference.PRIMARY
)

db = getattr(conn, settings.MONGO['DB'])

(and as a point of curiousity, is this the right way to do connection pooling in pymongo?)

1条回答
甜甜的少女心
2楼-- · 2019-04-02 15:38

While this won't fix your issue, the hang was introduced in July 2012 on this commit to pymongo: https://github.com/mongodb/mongo-python-driver/commit/1fe6029c5d78eed64fcb2a6d368d9cdf8756d2f4#commitcomment-1820334.

Specifically, it only affects ReplicaSetConnections. The answer they gave is to call connection.close(), but as you correctly pointed out in your question, there is no good hook to close the connection.

I believe that you can safely close the connection at the end of every request. Django already does this for its ORM connections to the db. This is why they recommending using a connection pool like pgbouncer, so reconnecting to postgres is instant. Pymongo has a connection pool built in, so reconnect at will.

查看更多
登录 后发表回答