Write lock on a yaml file?
This post explains how to put file write lock on regular files.
B开发者_如何学编程ut is it possible to put a lock on a yaml file, so only one script at a time can write to it?
I am using YAML::Syck
, if that makes a difference.
You can lock a file using flock
. You'll need to open the file first as flock
operates on filehandles.
Alternatively you can use LockFile::Simple
from CPAN.
As others have said, use flock. Also, see perlfaq5 under "How can I lock a file".
It's important to note that it's advisory locking anyway, so there is no need to actually lock the .yaml file. You can just lock a lock file (maybe yourfile.yaml.lock) and unlock it when done.
This also gives you the capability to say "this block of operations requires exclusive access", rather than just limit your exclusivity to a single file.
精彩评论