无法在 pymongo 中打开安全的情况下插入

发布于 2024-12-09 22:02:12 字数 877 浏览 4 评论 0原文

以下代码运行没有问题。

collection.insert(file_cont)

以下代码运行w问题如下:

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

问题1>如何解决这个问题?

问题2>我应该使用 insert(sth, safe=True) 还是 insert(sth) ?

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> should I use insert(sth, safe=True) or insert(sth)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-12-16 22:02:12

您似乎正在尝试将太大的文件插入数据库。

根据 pymongo 文档, 安全参数意味着:

如果 safe 为 True,则将检查插入是否有错误,从而引发
如果发生则操作失败。安全插入等待响应
从数据库中获取,而普通插入则不然。

因此,理论上您应该期望引发 OperationFailure,但您获得的是 MemoryError标准 python 错误 这意味着:

当操作内存不足时引发,但情况可能会这样
仍然可以被拯救(通过删除一些对象)。相关值是
指示哪种(内部)操作耗尽内存的字符串。
请注意,由于底层内存管理架构
(C 的 malloc() 函数),解释器可能并不总是能够
从这种情况中完全恢复;尽管如此,它还是提出了一个
异常,以便可以打印堆栈回溯,以防失控
程序是原因。

当您说collection.insert(file_cont)有效时,也许代码有效,但在数据库中的插入却默默地失败了。您应该检查尝试插入数据库的内容的大小以及计算机上的可用内存。

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.

听风吹 2024-12-16 22:02:12

这和安全有什么关系?你的内存不足了(就像与愚蠢的Python编程有关)......完全不相关

What has this to do with safe? You are running out of memory (like related to stupid Python programming)...completely unrelated

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文