How do I lock a file in Perl?

前端 未结 14 537
礼貌的吻别
礼貌的吻别 2020-12-01 05:45

What is the best way to create a lock on a file in Perl?

Is it best to flock on the file or to create a lock file to place a lock on and check for a lock on the lock

相关标签:
14条回答
  • 2020-12-01 05:54

    Ryan P wrote:

    In this case the file is actually unlocked for a short period of time while the file is reopened.

    So don’t do that. Instead, open the file for read/write:

    open my $fh, '+<', 'test.dat'
        or die "Couldn’t open test.dat: $!\n";
    

    When you are ready to write the counter, just seek back to the start of the file. Note that if you do that, you should truncate just before close, so that the file isn’t left with trailing garbage if its new contents are shorter than its previous ones. (Usually, the current position in the file is at its end, so you can just write truncate $fh, tell $fh.)

    Also, note that I used three-argument open and a lexical file handle, and I also checked the success of the operation. Please avoid global file handles (global variables are bad, mmkay?) and magic two-argument open (which has been a source of many a(n exploitable) bug in Perl code), and always test whether your opens succeed.

    0 讨论(0)
  • 2020-12-01 05:56

    Here's my solution to reading and writing in one lock...

    open (TST,"+< readwrite_test.txt") or die "Cannot open file\n$!";
    flock(TST, LOCK_EX);
    # Read the file:
    @LINES=<TST>;
    # Wipe the file:
    seek(TST, 0, 0); truncate(TST, 0);
    # Do something with the contents here:
    push @LINES,"grappig, he!\n";
    $LINES[3]="Gekke henkie!\n";
    # Write the file:
    foreach $l (@LINES)
    {
       print TST $l;
    }
    close(TST) or die "Cannot close file\n$!";
    
    0 讨论(0)
  • 2020-12-01 06:02

    Use the flock Luke.

    Edit: This is a good explanation.

    0 讨论(0)
  • Have you considered using the LockFile::Simple module? It does most of the work for you already.

    In my past experience, I have found it very easy to use and sturdy.

    0 讨论(0)
  • 2020-12-01 06:05
    use strict;
    
    use Fcntl ':flock'; # Import LOCK_* constants
    
    # We will use this file path in error messages and function calls.
    # Don't type it out more than once in your code.  Use a variable.
    my $file = '/path/to/some/file';
    
    # Open the file for appending.  Note the file path is in quoted
    # in the error message.  This helps debug situations where you
    # have a stray space at the start or end of the path.
    open(my $fh, '>>', $file) or die "Could not open '$file' - $!";
    
    # Get exclusive lock (will block until it does)
    flock($fh, LOCK_EX);
    
    
    # Do something with the file here...
    
    
    # Do NOT use flock() to unlock the file if you wrote to the
    # file in the "do something" section above.  This could create
    # a race condition.  The close() call below will unlock it
    # for you, but only after writing any buffered data.
    
    # In a world of buffered i/o, some or all of your data will not 
    # be written until close() completes.  Always, always, ALWAYS 
    # check the return value on close()!
    close($fh) or die "Could not write '$file' - $!";
    
    0 讨论(0)
  • 2020-12-01 06:06

    Flock is probably the best but requires you to write all the supporting code around it - timeouts, stale locks, non-existant files etc. I trued LockFile::Simple but found it started setting the default umask to readonly and not cleaning this up. Resulting in random permissions problems on a multi process/multi-threaded application on modperl I've settled on wrapping up NFSLock with some empty file handling.

    0 讨论(0)
提交回复
热议问题