Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Unique filenames with Time::HiRes

by knoebi (Friar)
on Jul 19, 2004 at 07:53 UTC ( [id://375477]=note: print w/replies, xml ) Need Help??


in reply to Unique filenames with Time::HiRes

you could just add an additional integer to your filename, so it will be unique. if you start with 0 and go up, you even have the real order.

generate the filename, test if a file allready exists with this if not create one... you don't need locking for that.

anyway, if you open more files than time::hires can show, probably you should change to a database.

ciao
knoebi

UPDATE: you need locking, see beable's reply.

Replies are listed 'Best First'.
Re^2: Unique filenames with Time::HiRes
by beable (Friar) on Jul 19, 2004 at 08:43 UTC
    you could just add an additional integer to your filename, so it will be unique. if you start with 0 and go up, you even have the real order.

    generate the filename, test if a file allready exists with this if not create one... you don't need locking for that.

    Actually, you would need locking for that, unless you have an atomic test-and-create-file function. Otherwise, you could have two processes which end up in a race condition:
    Process 1 checks for filename XXX-42, doesn't exist Process 2 checks for filename XXX-42, doesn't exist Process 1 creates filename XXX-42 and starts writing to it Process 2 opens filename XXX-42 and starts writing to it Process 1 finishes writing to XXX-42 Process 2 finishes writing to XXX-42
    Then what's in the file? Who knows? Probably not what you want.
      You are correct about this. You have to look that File XX-42, but the other process don't have to wait until the process which has opened XX-42 finishs. This was part of his question.

Re^2: Unique filenames with Time::HiRes
by AcidHawk (Vicar) on Jul 19, 2004 at 08:08 UTC
    The filenames are created using a command line script that accepts parameters and builds the file. I need to keep the counter number somewhere else and not in the script itself.

    If I dont lock the 'counter number holder file thingy' and two occurances of the cli access the 'counter number holder file thingy', each occurance of the script will get the same number.. hence the requirement to lock the 'counter number holder file thingy'.

    I do agree though that I might need to look at a database.

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      Maybe just append a random number, lock it successfully, or try another.
        Maybe just append a random number

        Or maybe not.

        The OP states that the names must be in sequence. Consider two files created in the epoch second 1090225457. The first file is generated with the rand string 0.837 (rounded). Half a second later, still in the same epoch second, another file is created, and rand returns 0.238 (rounded). The second file will sort before the first...

        The same critism can be made of beable's solution elsewhere in this thread.

        - another intruder with the mooring of the heat of the Perl

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://375477]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (2)
As of 2024-04-19 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found