开发者

Python - HardDrive access when opening files

If you open a file for reading, read from it, close it, and then repeat the process (in a loop) does python continually access the hard-drive? Because it doesn't seem to from my experimentation, and I'd like to understand why.

An (extremely) simple example:

while True:
    file = open('/var/log/messages', 'r')
    stuff = file.read()
    file.close()
    time.sleep(2)

If I run this, my hard-drive access开发者_如何学编程 LED lights up once and then the hard-drive remains dormant. How is this possible? Is python somehow keeping the contents of the file stored in RAM? Because logic would tell me that it should be accessing the hard-drive every two seconds.


The answer depends on your operating system and the type of hard drive you have. Most of the time, when you access something off the drive, the information is cached in main memory in case you need it again soon. Depending on the replacement strategy used by your OS, the data may stay in main memory for a while or be replaced relatively soon.

In some cases, your hard drive will cache frequently accessed information in its own memory, and then while the drive might be accessed, it will retrieve the information faster and send it to the processor faster than if it had to search the drive platters.


Likely your operating system or file-system is smart enough to serve the file from the OS cache if the file has not changed in between.


Python does not cache, the operating system does. You can find out the size of these caches with top. In the third line, it will say something like:

Mem:   5923332k total,  5672832k used,   250500k free,    43392k buffers

That means about 43MB are being used by the OS to cache data recently written to or read from the hard disk. You can turn this caching off by writing 2 or 3 to /proc/sys/vm/drop_caches.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜