Forums

Avoiding filename collisions between web workers...

EDIT: RESOLVED

Hi!

I've written a small site using Flask and a bunch of other stuff. I'm uploading images to Imgur from my site, hosted on PythonAnywhere. Before I upload to Imgur, I create a temporary file.

The problem is that PythonAnywhere uses web workers to handle multiple incoming requests to the server. If I name the file 'temp.png' then the second worker could clobber (overwrite) the file before it gets uploaded to Imgur. So it seems what I need is a unique filename for the file, but I'm not sure how to go about guaranteeing no web worker generates the same filename (in the event that they are both running at roughly the same time).

How do I do this?

How did you solve it in the end? I would have recommended the tempfile module --

import tempfile
tf = tempfile.NamedTemporaryFile()

Hi, Harry. I actually used my own naming scheme here.

PID + milliseconds since epoch

that'll work! Unless, by an incredible coincidence, the task on two different servers manages to get the same pid (could happen!) and it runs at the exact same millisecond (much less likely ;)

Is there a way to do it which guarantees that it never happens?

I really wouldn't worry about it. The odds are astronomical. Still, if you're feeling paranoid, you could try using fcntl to get a lock on the file you want, to reassure yourself you have exclusive access to it. If you do a bit of reading around, you'll find out that posix locks are a bit weird, especially over NFS, but you should be able to make it work if you dig into it...