not able to insert with safe turned on in pymongo
The following code runs w/o problems.
collection.insert(file_cont)
The following code runs w problems as follows:
collection.insert(file_cont, safe=True)
Traceback (most recent call last):
File "/home/user/Documents/Python/Python_MongoDB/connect_db.py", line 102, in <module>
patterns="*.[zZ][iI][pP]")
File "/home/user/Documents/Python/Python_MongoDB/connect_db.py", line 93, in fs_load_data_to_db
collection.insert(file_cont, safe=True)
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1-py2.7-linux-i686.egg/pymongo/collection.py", line 283, in insert
check_keys, safe, kwargs), safe)
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1-py2.7-linux-i686.egg/pymongo/message.py", line 75, in insert
data += "".join(encoded)
MemoryError
Question 1> How to fix this problem?
Question 2> sho开发者_如何学Gould I use insert(sth, safe=True) or insert(sth)?
It looks like you're trying to insert a too big file into your database.
According to the pymongo documentation, the safe
parameter means :
If safe is True then the insert will be checked for errors, raising OperationFailure if one occurred. Safe inserts wait for a response from the database, while normal inserts do not.
So, in theory you should expect an OperationFailure
to be raised, but what you obtain is a MemoryError
, a standard python error that means :
Raised when an operation runs out of memory but the situation may still be rescued (by deleting some objects). The associated value is a string indicating what kind of (internal) operation ran out of memory. Note that because of the underlying memory management architecture (C’s malloc() function), the interpreter may not always be able to completely recover from this situation; it nevertheless raises an exception so that a stack traceback can be printed, in case a run-away program was the cause.
When you said that collection.insert(file_cont)
works, maybe the code works, but the insertion in database silently fails. You should check the size of what you try to insert in your database, and the available memory you have on your computer.
What has this to do with safe? You are running out of memory (like related to stupid Python programming)...completely unrelated
精彩评论