So I’ve made a gallery web page that supports uploading several images in one go. The problem is that I’ve made it so that any image that gets uploaded gets a file name like [username]_[unix timestamp].jpg
When viewing all uploaded images, the images are sorted by date. Initially this was a problem when uploading a lot of images because they all got very similar datetime stamps. Down to the same second. I was using get_next_by_date() and get_previous_by_date() to browse the images, and this did not work well when many images was stored down to the same second.
Another problem was that since the image processing was done with a speed of typically three images per second made three and three images get the same unix timestamp. This caused the same image that was the first to be processed in a second be represented three times on the site instead of the other images that was processed later that same second. (Operating System gave the files a different name when it found that the name was already taken, but the database still stored the original timestamp to filenames).
So far I’ve “fixed” the problem by popping a time.sleep(1) after each image is processed to guarantee a new second being started for each image, but this just makes me feel bad. 60 images to be uploaded is then guaranteed to take 60 seconds, while it shouldn’t have to take more than maybe 10-20 seconds.
Can anyone think of a solution to this? How can I make sure that images that are sorted on date can get the correct sequence when viewing images, and that the unix timestamp gets unique for all images?
You can create filename from timestamp using millisecond as well.
Using
strftimewith millisecond to create string for filename: