简体   繁体   English

Python中的MemoryError导致空泡菜文件

[英]MemoryError in Python leads to empty pickle file

I'm working on a library-type checkout/checkin system. 我正在使用库类型的结帐/签入系统。 When the user clicks exit , the program calls a close_window function which dumps the current dictionary objects into pickle files before the window is destroyed. 当用户单击exit ,程序将调用close_window函数,该函数将当前字典对象转储到pickle文件中,然后销毁窗口。

def close_window(self):
    if messagebox.askokcancel("Quit", "You want to close the program now?"):
        patrons.dump_data()

        self.master.destroy()

When the program is started again, it calls a load_data function which loads the pickled files. 再次启动该程序时,它将调用load_data函数,该函数将加载已腌制的文件。 Somehow I ran into MemoryError when exiting the system and one of the pickled files was overwritten with an empty file. 退出系统时,我不知何故遇到了MemoryError ,其中一个腌制文件被一个空文件覆盖。 From the documentation , I gather that MemoryError occurs when the program creates too many objects and runs out of memory. 文档中 ,我收集到当程序创建太多对象并用完内存时发生MemoryError I am not sure why this happened in my case since I am not dealing with large data. 由于我不处理大数据,因此我不确定为什么会发生这种情况。 The pickled file that got overwritten was only 1 KB. 被覆盖的腌制文件只有1 KB。

How can I ensure my pickled file is not overwritten with an empty file when MemoryError occurs? 发生MemoryError时,如何确保腌制的文件不会被空文件覆盖? This can lead to serious data loss. 这会导致严重的数据丢失。 I am new to programming and am using this project to learn. 我是编程新手,正在使用此项目进行学习。 It is possible I've done something seriously wrong to lead to the memory error or maybe I just need more computer memory. 我可能做了一些严重的错误而导致内存错误,或者我只需要更多的计算机内存。 In any case, it doesn't make sense to overwrite a saved file with an empty file whether memory error or not. 无论如何,无论是否有内存错误,用空文件覆盖保存的文件都是没有意义的。

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python34\lib\tkinter\__init__.py", line 1487, in __call__
    return self.func(*args)
  File "C:/Python34/Lib/site-packages/toolkit/main.py", line 435, in close_window
    patrons.dump_data()
  File "C:\Python34\Lib\site-packages\toolkit\patrons.py", line 20, in dump_data
    pickle.dump(patronslist, f)
MemoryError

This error is partially discussed in MemoryError while pickling data in python . 在python腌制数据时,此错误在MemoryError中进行了部分讨论。 Here though, I got an empty file, not even a partial file. 不过,在这里,我得到了一个空文件,甚至没有部分文件。 And I want to know if there is a workaround for this problem. 我想知道是否有解决此问题的方法。 Perhaps saving the pickled data to a temporary file. 也许将腌制的数据保存到临时文件中。 If no memory error occurred during the save, then the temp file can be used to overwrite the permanent file (but this may yet trigger another MemoryError right?). 如果在保存过程中没有发生内存错误,则可以使用临时文件覆盖永久文件(但这可能会触发另一个MemoryError吗?)。

I run Win7 x86, 3 GB RAM, Python 3.4.1 我运行Win7 x86、3 GB RAM,Python 3.4.1

Based on Gerrat's comment above, I wonder if the following is a good way to go about this: 基于以上Gerrat的评论,我想知道以下方法是否是解决此问题的好方法:

patrons.py
def dump_data():
    with open("./pickled_dicts/temp_patrons.pkl", 'wb') as f:
        global patronslist
        pickle.dump(patronslist, f)


main.py
def close_window(self):
    if messagebox.askokcancel("Quit", "You want to close the program now?"):
        try:
            patrons.dump_data()
            os.remove("./pickled_dicts/patrons.pkl")
            os.rename("./pickled_dicts/temp_patrons.pkl", "./pickled_dicts/patrons.pkl")
        except MemoryError:
            messagebox.showerror("Memory Problem", "Your computer experienced memory problem. Your last session was not saved.")     
        self.master.destroy()

Essentially, I am first saving the dictionary object to a temporary file ( temp_patrons.pkl ) which is renamed to my permanent file ( patrons.pkl ) assuming no MemoryError . 本质上,我首先将字典对象保存到一个临时文件( temp_patrons.pkl ),假定没有MemoryError ,该文件将重命名为我的永久文件( patrons.pkl )。 If MemoryError , then the original patrons.pkl remains. 如果为MemoryError ,则保留原始的patrons.pkl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM