如何关闭mongodb python连接?

时间:2021-07-12 02:32:49

I'm doing a python script that writes some data to a mongodb. I need to close the connection and free some resources, when finishing.

我正在做一个将一些数据写入mongodb的python脚本。完成后,我需要关闭连接并释放一些资源。

How is that done in Python?

如何在Python中完成?

1 个解决方案

#1


64  

Use close() method on your MongoClient instance:

在MongoClient实例上使用close()方法:

client = pymongo.MongoClient()

# some code here

client.close()

close() is an alias for disconnect() method:

close()是disconnect()方法的别名:

Disconnecting will close all underlying sockets in the connection pool. If this instance is used again it will be automatically re-opened.

断开连接将关闭连接池中的所有底层套接字。如果再次使用此实例,它将自动重新打开。

#1


64  

Use close() method on your MongoClient instance:

在MongoClient实例上使用close()方法:

client = pymongo.MongoClient()

# some code here

client.close()

close() is an alias for disconnect() method:

close()是disconnect()方法的别名:

Disconnecting will close all underlying sockets in the connection pool. If this instance is used again it will be automatically re-opened.

断开连接将关闭连接池中的所有底层套接字。如果再次使用此实例,它将自动重新打开。