/ devops

Lock writes to a mongodb server

Using the mongo shell

check status

db.current_op()

Lock

db.fsyncLock()

UnLock

db.fsyncUnlock()

Using pymongo

Note: lock/unlock must be done using the same connection/session

mongo_client = mongo_connect(mongo_host, mongo_user,
                             mongo_pass)

def mongo_fsync_lock(conn, state):
        if state is True:
             conn.fsync(lock=True)
        if state is False:
                conn.unlock()
        print "Lock state: %s" % conn.is_locked

#to lock
mongo_fsync_lock(mongo_client, True)
#to unlock
mongo_fsync_lock(mongo_client, False)