我知道打开一个文件只会创建一个文件处理程序,它占用一个固定的内存,而与文件大小无关。
Django有一个名为InMemoryUploadedFile
的类型,表示通过表单上载的文件。
我在django视图中获得我的file对象的句柄,如下所示:
file_object = request.FILES["uploadedfile"]
此文件对象具有类型InMemoryUploadedFile
。
现在我们可以自己看到,file_对象有一个方法.read()
,该方法用于将文件读入内存。
bytes = file_object.read()
类型为InMemoryUploadedFile
的文件对象不是已经“在内存中”了吗?
要考虑的一件事是,在python中,类似文件的对象有一个API,这个API是严格遵守的。这使得代码非常灵活,它们是I/O流上的抽象。这些允许您的代码不必担心数据来自何处,即内存、文件系统、网络等
File like对象通常定义一对methods,其中之一是
read
我不确定
InMemoryUploadedFile
的实际实现,也不确定它们是如何生成的,也不知道它们存储在哪里(我假设它们完全在内存中),但您可以放心,它们是类似于文件的对象,并且包含一个read
方法,因为它们遵循文件api。对于实现,您可以开始检查源:
文件对象上的
read()
方法是从文件对象中访问内容的方法,而不管该文件是在内存中还是存储在磁盘上。它类似于其他实用程序文件访问方法,如readlines
或seek
。这种行为类似于built intoPython,后者反过来是通过操作系统的
fread()
方法构建的。关于
InMemoryUploadedFile
确切存储在哪里的问题,它是一个bit more complicated。相关问题 更多 >
编程相关推荐