15👍
There’s no need to close a Connection
instance, it will clean up after itself when Python garbage collects it.
You should use MongoClient
instead of Connection
; Connection
is deprecated. To take advantage of connection pooling, you could create one MongoClient
that lasts for the entire life of your process.
PyMongo represents documents as dicts. Why are you encoding each dict it gives you as JSON, then decoding it again? It may be more efficient to modify the objects directly.
That said, I agree with user3683180 that the real problem–the reason MongoDB is taking so much CPU–is in your schema or index design, not in your Python code.
8👍
Given the name of your database ‘indexer’, and the ‘unique’ property which requires an index, I’m thinking your CPU usage might have nothing to do with this code.
Try using mongostat and mongotop to see what mongo is spending its time doing.. I’m thinking you’ll find it’s spending time crunching data and that your code is just fine.
- How to configure Apache to run ASGI in Django Channels? Is Apache even required?
- Django limit_choices_to on user group
- Django datefield and timefield to python datetime
- Confused by Django's claim to MVC, what is it exactly?
- Django: conditional expression