Vandana
Vandana

Reputation: 137

How can I store lock objects in MongoDB?

I have a put and get function that inserts into MongoDB:

def put(self,key,value):
    key = self.path+"&&"+key
    value1 = Binary(pickle.dumps(value))
    entry = {"keyname":key,"info":value1}      
    self.filenode.update({"keyname":key}, { "$set" : entry }, upsert=True)

def get(self,key):
    key1 = key
    key = self.path+"&&"+key
    res = self.filenode.find_one({"keyname":key})        
    if "info" in res:
        x = res["info"]
        res1 = pickle.loads(x)
        return res1
    else:
        return None

This works if value in put is of simple type such as string, dict etc. But if value is an object it is not able to pickle inside the put function. The error I'm getting is:

raise TypeError, "can't pickle %s objects" % base.__name__

TypeError: can't pickle lock objects

Upvotes: 1

Views: 574

Answers (2)

Mike McKerns
Mike McKerns

Reputation: 35247

You could use a better serializer like dill, which can pickle a Lock and most objects in python.

>>> import threading
>>> l = threading.Lock()
>>> 
>>> import dill
>>> dill.dumps(l)
'\x80\x02cdill.dill\n_create_lock\nq\x00\x89\x85q\x01Rq\x02.'

Then you can save most objects to a database, by first converting to a pickled string.

Upvotes: 3

pourhaus
pourhaus

Reputation: 616

Have a read of Python's documentation on serialization - pickle — Python object serialization. If you control the definition of the object, you can provide a __getstate__() method that can remove un-serializable attributes like locks.

I use this to set a lock to None then use __setstate__() as a de-serializing hook to re-initialize the lock.

Upvotes: 1

Related Questions