mongokit
mongokit copied to clipboard
How to close connections?
I can not find the method closed conntions,when I run program, the conntions growing fast until mongodb conntions full and then I can not connect to mongodb, how can I close connections?
@sushuai It's simple to close connections opened by MongoKit. Here are my solution:
# create a connection
connection = Connection()
# do something
...
# close the connection
connection.close()
And even more, if you use MongoKit in a Flask-RESTful project, it's also very simple to close connections opened by each API request:
from flask import Flask
from flask_restful import Api
app = Flask(__name__)
api = Api(app)
@app.teardown_request
def teardown_request(exception): # close db connection after each api request
connection.close()