有没有一种简单的方法可以在架子上使用 python 临时文件(并确保它自行清理)?
基本上,我想要在我正在编写的 python 程序中使用无限大小(更准确地说,硬盘驱动器而不是内存限制)的字典。看起来 tempfile 和 shelve 模块自然适合于此,但是,我不知道如何以安全的方式一起使用它们。我希望在搁置被 GC 时删除临时文件(或者在搁置不再使用后保证删除,无论何时),但我能想到的唯一解决方案涉及使用 tempfile.TemporaryFile() 打开文件句柄,从句柄获取文件名,使用此文件名打开搁置,保留对文件句柄的引用以防止其被 GC(并删除文件),然后将包装器放在存储此搁置的搁架上参考。有人有比这个复杂的混乱更好的解决方案吗?
限制:只能使用标准python库并且必须完全跨平台。
Basically, I want an infinite size (more accurately, hard-drive rather than memory bound) dict in a python program I'm writing. It seems like the tempfile and shelve modules are naturally suited for this, however, I can't see how to use them together in a safe manner. I want the tempfile to be deleted when the shelve is GCed (or at guarantee deletion after the shelve is out of use, regardless of when), but the only solution I can come up with for this involves using tempfile.TemporaryFile() to open a file handle, getting the filename from the handle, using this filename for opening a shelve, keeping the reference to the file handle to prevent it from getting GCed (and the file deleted), and then putting a wrapper on the shelve that stores this reference. Anyone have a better solution than this convoluted mess?
Restrictions: Can only use the standard python library and must be fully cross platform.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我宁愿继承 shelve.Shelf,并重写 close 方法 (*) 以取消链接文件。请注意,根据所使用的特定 dbm 模块,您可能有多个包含架子的文件。一种解决方案可能是创建一个临时目录,而不是临时文件,并在完成后删除该目录中的所有内容。另一种解决方案是绑定到特定的 dbm 模块(例如 bsddb 或 dumbdbm),并专门删除这些库创建的那些文件。
(*) 请注意,当架子被垃圾收集时,也会调用架子的 close 方法。唯一可能产生垃圾文件的情况是解释器崩溃或被杀死。
I would rather inherit from shelve.Shelf, and override the close method (*) to unlink the files. Notice that, depending on the specific dbm module being used, you may have more than one file that contains the shelf. One solution could be to create a temporary directory, rather than a temporary file, and remove anything in the directory when done. The other solution would be to bind to a specific dbm module (say, bsddb, or dumbdbm), and remove specifically those files that these libraries create.
(*) notice that the close method of a shelf is also called when the shelf is garbage collected. The only case how you could end up with garbage files is when the interpreter crashes or gets killed.