django_dev_101
django_dev_101

Reputation: 21

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.

Upvotes: 2

Views: 1880

Answers (3)

user2665694
user2665694

Reputation:

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.

Upvotes: 3

phihag
phihag

Reputation: 287885

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.

Upvotes: 1

Chris Gregg
Chris Gregg

Reputation: 2382

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.

Upvotes: 2

Related Questions