TheMeaningfulEngineer
TheMeaningfulEngineer

Reputation: 16319

Limiting the lifetime of a file in Python

Helo there,

I'm looking for a way to limit a lifetime of a file in Python, that is, to make a file which will be auto deleted after 5 minutes after creation.


Problem:

I have a Django based webpage which has a service that generates plots (from user submitted input data) which are showed on the web page as .png images. The images get stored on the disk upon creation.

Image files are created per session basis, and should only be available a limited time after the user has seen them, and should be deleted 5 minutes after they have been created.


Possible solutions:

  1. I've looked at Python tempfile, but that is not what I need, because the user should have to be able to return to the page containing the image without waiting for it to be generated again. In other words it shouldn't be destroyed as soon as it is closed

  2. The other way that comes in mind is to call some sort of an external bash script which would delete files older than 5 minutes.

Does anybody know a preferred way doing this?

Ideas can also include changing the logic of showing/generating the image files.

Upvotes: 4

Views: 2560

Answers (2)

Mp0int
Mp0int

Reputation: 18727

Ok that might be a good approach i guess...

You can write a script that checks your directory and delete outdated files, and choose the oldest file from the un-deleted files. Calculate how much time had passed since that file is created and calculate the remaining time to deletion of that file. Then call sleep function with remaining time. When sleep time ends and another loop begins, there will be (at least) one file to be deleted. If there is no files in the directory, set sleep time to 5 minutes.

In that way you will ensure that each file will be deleted exactly 5 minutes later, but when there are lots of files created simultaneously, sleep time will decrease greatly and your function will begin to check each file more and more often. To aviod that you add a proper latency to sleep function before starting another loop, like, if the oldest file is 4 minutes old, you can set sleep to 60+30 seconds (adding all time calculations 30 seconds).

An example:

from datetime import datetime
import time
import os

def clearDirectory():
    while True:
        _time_list = []
        _now = time.mktime(datetime.now().timetuple())
        for _f in os.listdir('/path/to/your/directory'):
            if os.path.isfile(_f):
                _f_time = os.path.getmtime(_f) #get file creation/modification time
                if _now - _f_time < 300:
                    os.remove(_f) # delete outdated file
                else:
                    _time_list.append(_f_time) # add time info to list
        # after check all files, choose the oldest file creation time from list
        _sleep_time = (_now - min(_time_list)) if _time_list else 300 #if _time_list is empty, set sleep time as 300 seconds, else calculate it based on the oldest file creation time
        time.sleep(_sleep_time)

But as i said, if files are created oftenly, it is better to set a latency for sleep time

time.sleep(_sleep_time + 30) # sleep 30 seconds more so some other files might be outdated during that time too...

Also, it is better to read getmtime function for details.

Upvotes: 1

webjunkie
webjunkie

Reputation: 7039

You should write a Django custom management command to delete old files that you can then call from cron.

If you want no files older than 5 minutes, then you need to call it every 5 minutes of course. And yes, it would run unnecessarily when there are no users, but that shouln't worry you too much.

Upvotes: 2

Related Questions