Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Concurrent Cache Pattern

by flexvault (Monsignor)
on Aug 04, 2012 at 10:40 UTC ( #985426=note: print w/replies, xml ) Need Help??


in reply to Concurrent Cache Pattern

pileofrogs,

Because of your concern about a 'flock' on the lock from a runaway process, you can use 2 files, one for the locking and one for the information your caching. I'm going to show untested code to give you an idea of how to go about it, but only you can decide what to do in a true deadlock situation.

my $lock_file = "./LockFile"; ## Put this in a common place GetLock: if ( ! -e $lock_file ) ## No Process is working { open ( my $lock, ">", $lock_file ) or die "Can't open $! "; if ( flock ( $lock, LOCK_EX | LOCK_NB ) ) { print $lock "$$\n"; close $lock; ## Now, you need to make sure your the process with the lock open ( $lock, "<", $lock_file ) or die "Can't open $! "; if ( flock ( $lock, LOCK_SH ) ) { my $pid = < $lock >; chomp ( $pid ); close $lock; if ( $pid eq "$$" ) { last; } ## Good--you have the lo +ck else { redo GetLock; } } else { redo GetLock; } } else { ## Another process has the exclusive lock close $lock; usleep 1_000; ## wait .001 seconds redo GetLock; } } elsif { if ( -s $lock_file ) ## Another Process is already w +orking { open ( my $lock, "<", $lock_file ) or die "Can't open $! " +; my $who = < $lock >; chomp ( $who ); close $lock; ## Now you can stat the file and if it exists longer than ## some number of seconds/minutes, you have the pid to k +ill it. } else { redo GetLock; } } ## Put your testing and cache here.

The idea is to verify you have the lock before starting the cache process. Once you are sure you have the lock, you can test the cache file to determine if it is fresh or stale. When you finish your work you 'unlink' the lock file.

Things to think about:

  • Do you need a counter on how many times to 'redo GetLock'
  • Do you need to issue an warning email/text message for deadlock.
  • Instead of using 'die' to have a common routine to issue warning message to the system admin.
  • Maybe BrowserUk's solution is better!

Good Luck!

"Well done is better than well said." - Benjamin Franklin

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985426]
help
Chatterbox?
[marto]: "no it's not that"...."weirdos "...
[marto]: List EXE_FILES installed by CPAN so a couple of people suggest that your code looks obfuscated. I'd have to ageree, from the perspective of those who can't follow all of that one liner, it doesn't read well
Veltro is a weirdo, obsessed with whitespace
[marto]: to use the word "obsession" when so few people have said so little about it is grasping at staws
marto wishes tye was around, he's so much better at this sort of thing
usemodperl likes tye!
usemodperl tye  too
[choroba]: Re^3: LiBXML: New markup while preserving earlier tags? would benefit from a couple of test cases
usemodperl meant tye&nbsp;
usemodperl pokes Veltro with line noise

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (8)
As of 2018-06-24 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?