Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Unique filenames with Time::HiRes

by DrHyde (Prior)
on Jul 19, 2004 at 08:52 UTC ( [id://375495]=note: print w/replies, xml ) Need Help??


in reply to Unique filenames with Time::HiRes

As you've seen, using the time to generate unique filenames doesn't work. A common work-around is to concatenate the time and the process ID (you're only generating one per process, right?), the reasoning being that it's not possible to have two processes with the same ID at the same time.

In theory, it might be possible to spawn processes fast enough and which die fast enough to cycle through all the available IDs in one second, so that's not really a very good solution either.

I would recommend that you look at Data::UUID which claims to be an implementation of a standard method which is guaranteed to work, and which claims to support allocating very large numbers of IDs.

Replies are listed 'Best First'.
Re^2: Unique filenames with Time::HiRes
by adrianh (Chancellor) on Jul 19, 2004 at 09:21 UTC
    I would recommend that you look at Data::UUID

    I'd second that. Works very well in my experience. Use the creation date of the files to order them.

    (I'm assuming that your comments about multiple processes mean that the exact order isn't important.)

      Use the creation date of the files to order them.
      Since he can create several files in a short interval, so that even Time::HiRes isn't updated quickly enough (update interval is 18 times a second, on Windows), and the resolution of creation date is in general only 1 or 2 seconds, I am certain you haven't solved the OP's problem.
        Since he can create several files in a short interval, so that even Time::HiRes isn't updated quickly enough (update interval is 18 times a second, on Windows), and the resolution of creation date is in general only 1 or 2 seconds, I am certain you haven't solved the OP's problem

        I am less certain of your certainty ;-)

        The OP apparently has multiple processes running that are dumping these files (my emphasis):

        command line interface gets called extremely often and often several for these processes run at the same time

        Which was why I said:

        (I'm assuming that your comments about multiple processes mean that the exact order isn't important.)

        The exact running order is going to be dependent on a lot of factors outside the OPs control. Depending on the exact requirements having unique filenames and a rough order may well be enough.

Re^2: Unique filenames with Time::HiRes
by AcidHawk (Vicar) on Jul 19, 2004 at 09:04 UTC
    This is exactally what I need to do.

    I will create files that look like XXX~gettimeofday()~PID.xml and sort on the time of day. If there are two files with the same time of day I will have to build in some kind of exception handeling for the sequence thing.

    Thank you!

    -----
    Of all the things I've lost in my life, its my mind I miss the most.
      Cycling through all the PIDs within a second (or whatever is the smallest increment of time you can get) is not the problem. Hitting that cycle in one of those itervals is. And it will happen.

      So what you need to do for your exception handling is to first determine how high the process counter on your machine can count (this may possibly change with OS updates, so I'd try to get that information dynamically from the OS). Then decide that if there is a difference of for examle at least half that maximum count between PIDs in your filenames, the lower block of them came after the higher block. Process them accordingly

      That should do ya.

      Update: grinder has /msged me that this will not work on BSD type boxes where random PID allocation (a security feature) has been configured. He's probably right, so you better talk to your admin before relying on consecutive PID allocation for the next couple of years. :-)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (1)
As of 2024-04-25 00:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found