How do I insert async to a log in mongodb?

asynchronous, mongodb, pymongo, python

Solution

Because you're passing a `w=0` parameter to `insert`, the operation is non-blocking and the call will simply queue the document for insert and return.

And leave the connection open for best performance.

Problem

I want to use PyMongo as a logger for a Django app. I don't mind if some inserts in the log table are lost, so I want to send a log to mongodb in another server, and continue the execution without waiting for confirmation. I am reading pymongo docs, but it's not clear to me if the inserts in a collection are blocking or not. I'm thinking of doing this inside a django model method ``` from pymongo import MongoClient conn = MongoClient('mongoserver', 27017) db = conn.main col = db.log col.insert({"user": "Pedro", "action": "search", "Origin": "Katmandu"}, w=0) conn.close() ``` I don't know if I the insert is async like that and if the connection should be closed or not

Original source