What is the best practice for creating MongoDB connections on python?

Viewed 183

I store a large amount of time series data into MongoDB. For this purpose I use multiprocessing in python. I have also written several queries on these data. For inserting data into MongoDB and also for each query, I used a separate MongoDB connection like this:

db_client = MongoClient(self.service_name, self.port_number)

However, another solution is to define MongoClient one time as a global variable and using it in different functions. What is the best way to creating mongo connection in python for different functions and classes? Thanks

0 Answers
Related