MongoEngine:关闭连接 [英] MongoEngine: Close connection

查看:369
本文介绍了MongoEngine:关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我花了很长时间试图找到一个简单的示例,其中使用了MongoEngine并关闭了连接.终于找到答案并发布了我的代码.

I spent ages trying to find a simple example where MongoEngine was being used and a connection was being closed. Finally figured it out and posting my code.

推荐答案

我知道这是一个老问题,但是如果有人在搜索,我想我会给出一个替代答案.

I know this is an old question, but if anyone else is searching I figured I'd give an alternate answer.

close()实际上并没有从MongoEngine的连接列表中删除该连接.稍后尝试连接到其他数据库时,这会导致问题.

close() does not actually remove the connection from MongoEngine's connection list. This causes problems when trying to connect to a different database later on.

为解决这个问题,我使用了mongoengine.connection.disconnect(即使它未在__all__中列出).我的代码如下:

To solve this I used mongoengine.connection.disconnect (even though it's not listed in __all__). My code looks like this:

from mongoengine import connect
from mongoengine.connection import disconnect

db = connect(alias='some_alias')

{do stuff}

disconnect(alias='some_alias')

您也可以不使用别名,因为在连接和断开连接时,别名都将默认设置为默认".

You can also leave the alias out as it will default to 'default' in both connect and disconnect.

这篇关于MongoEngine:关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆