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:
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 time.pid.incrementer, 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?).... my $append = "a"; while ( -e $id[$i] ) { $id[$i] =~ s/(?:\.[a-z]+)*$/.$append/; $append++; } `touch $id[$i]`; ...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Re: Use time() to create unique ID
by BrowserUk (Patriarch) on Sep 17, 2003 at 07:16 UTC |
In Section
Seekers of Perl Wisdom