是不是真的“在记忆中”?

2024-10-06 11:19:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道打开一个文件只会创建一个文件处理程序,它占用一个固定的内存,而与文件大小无关。 Django有一个名为InMemoryUploadedFile的类型,表示通过表单上载的文件。

我在django视图中获得我的file对象的句柄,如下所示:

file_object = request.FILES["uploadedfile"]

此文件对象具有类型InMemoryUploadedFile

现在我们可以自己看到,file_对象有一个方法.read(),该方法用于将文件读入内存。

bytes = file_object.read()

类型为InMemoryUploadedFile的文件对象不是已经“在内存中”了吗?


Tags: 文件对象django方法内存视图处理程序表单
2条回答

要考虑的一件事是,在python中,类似文件的对象有一个API,这个API是严格遵守的。这使得代码非常灵活,它们是I/O流上的抽象。这些允许您的代码不必担心数据来自何处,即内存、文件系统、网络等

File like对象通常定义一对methods,其中之一是read

我不确定InMemoryUploadedFile的实际实现,也不确定它们是如何生成的,也不知道它们存储在哪里(我假设它们完全在内存中),但您可以放心,它们是类似于文件的对象,并且包含一个read方法,因为它们遵循文件api。

对于实现,您可以开始检查源:

文件对象上的read()方法是从文件对象中访问内容的方法,而不管该文件是在内存中还是存储在磁盘上。它类似于其他实用程序文件访问方法,如readlinesseek

这种行为类似于built intoPython,后者反过来是通过操作系统的fread()方法构建的。

Read at most size bytes from the file (less if the read hits EOF before obtaining size bytes). If the size argument is negative or omitted, read all data until EOF is reached. The bytes are returned as a string object. An empty string is returned when EOF is encountered immediately. (For certain files, like ttys, it makes sense to continue reading after an EOF is hit.) Note that this method may call the underlying C function fread() more than once in an effort to acquire as close to size bytes as possible. Also note that when in non-blocking mode, less data than was requested may be returned, even if no size parameter was given.

关于InMemoryUploadedFile确切存储在哪里的问题,它是一个bit more complicated

Before you save uploaded files, the data needs to be stored somewhere.

By default, if an uploaded file is smaller than 2.5 megabytes, Django will hold the entire contents of the upload in memory. This means that saving the file involves only a read from memory and a write to disk and thus is very fast.

However, if an uploaded file is too large, Django will write the uploaded file to a temporary file stored in your system’s temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload. If an upload is large enough, you can watch this file grow in size as Django streams the data onto disk.

These specifics – 2.5 megabytes; /tmp; etc. – are simply “reasonable defaults”. Read on for details on how you can customize or completely replace upload behavior.

相关问题 更多 >