Django预关闭挂钩关闭挂pymongo连接

时间:2021-09-26 03:00:00

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.

我在Django项目中使用pymongo,最近我开始遇到一个问题,在退出主Django进程(即使通过管理命令)时,pymongo连接将挂起,并且进程永远不会退出。显然,堆栈中的某处出现了错误,但是现在最好的解决方案似乎是在Django退出之前显式关闭连接。

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

那么:有没有Django为此提供的关闭前信号或挂钩?

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

BTW:我的连接代码,以防你感兴趣。

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?)

(作为一个好奇的点,这是在pymongo中进行连接池的正确方法吗?)

1 个解决方案

#1


1  

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.

虽然这不会解决您的问题,但是在2012年7月对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.

具体来说,它只影响ReplicaSetConnections。他们给出的答案是调用connection.close(),但正如你在问题中正确指出的那样,关闭连接没有好的钩子。

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.

我相信您可以在每个请求结束时安全地关闭连接。 Django已经为它与db的ORM连接做了这个。这就是为什么他们建议使用像pgbouncer这样的连接池,因此重新连接到postgres是即时的。 Pymongo内置了一个连接池,因此可以随意重新连接。

#1


1  

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.

虽然这不会解决您的问题,但是在2012年7月对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.

具体来说,它只影响ReplicaSetConnections。他们给出的答案是调用connection.close(),但正如你在问题中正确指出的那样,关闭连接没有好的钩子。

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.

我相信您可以在每个请求结束时安全地关闭连接。 Django已经为它与db的ORM连接做了这个。这就是为什么他们建议使用像pgbouncer这样的连接池,因此重新连接到postgres是即时的。 Pymongo内置了一个连接池,因此可以随意重新连接。