laike9m
laike9m

Reputation: 19388

PyMongo: Should I use single or multiple clients?

The question is simple: should I keep all operations to a single MongoClient? Is single client or multiple clients better than the other?

Upvotes: 5

Views: 3518

Answers (1)

Markus W Mahlberg
Markus W Mahlberg

Reputation: 20712

The MongoClient actually maintains a connection pool. So having multiple clients does not have any advantages. Quite the contrary, since a new client has to connect to MongoDB first, which requires a three way handshake and other overhead each time a new client is created.

Since multiple clients only have disadvantages for a single application, the answer is: Only create one client and use it everywhere you need to make a connection.

Upvotes: 10

Related Questions