Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Re: Re: Use time() to create unique ID

by graff (Chancellor)
on Sep 17, 2003 at 05:43 UTC ( #292059=note: print w/replies, xml ) Need Help??

in reply to Re: Use time() to create unique ID
in thread Use time() to create unique ID

Of all the stuff I've seen on the current thread, this idea seems easiest and most sensible. And if there were going to be multiple instances of the same script running at once, all you need to do is add the pid ($$) to the file name as well.

Another way to avoid name collisions is to work out a unique name in a loop like this:

... my $append = "a"; while ( -e $id[$i] ) { $id[$i] =~ s/(?:\.[a-z]+)*$/.$append/; $append++; } `touch $id[$i]`; ...
Use a separate semaphore file to lock up this part, if necessary, to avoid race conditions among concurrent jobs. But this is probably overkill for the OP's task (and it takes more overhead -- without adding any real value -- relative to the simple, which would only risk collision if concurrent processes were writing to one shared directory from different hosts, and happened to have the same pids at the same time -- wow, how unlikely is that?).

Replies are listed 'Best First'.
Re: Re: Re: Use time() to create unique ID
by BrowserUk (Pope) on Sep 17, 2003 at 07:16 UTC

    <voice id="Lt.Cmdr Data"> The odds are approximately 3,279,967,300,002 to 1</voice>

    <voice id="Q"> You humans have such a limited concept of space and time! You didn't allow for alternate unverses where what you term "Quantum Mechanics" is the everyday norm</voice>

    <voice id="Major S. Carter">Wouldn't we'd also have to consider the effects of parallel universes?</voice>

    <voice ID="Albert Einstein">And to tink zey laughed at my theoriezzzz.</voice>

    <voice ID="Stephen Hawkin">The- whole - thing - can - be - easily - visualised - in -terms - of - the - likelyhood - of - two - billard - balls - dropping - into - the - same - pocket - at - the - same - time - from - the - breakoff.</voice>

    <voice ID="Z. Beedlebrox">Now if only we could make the destination of the Infinite Probablity Drive a little more predicable..</voice>

    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2021-03-05 14:51 GMT
Find Nodes?
    Voting Booth?
    My favorite kind of desktop background is:

    Results (112 votes). Check out past polls.