I've been trying to figure out how to do file locking in Raku without success. I started looking into fcntl with NativeCall, but then realized that fcntl locks don't prevent file access from other threads. What's the best way to do file locking in Raku?
Asked
Active
Viewed 170 times
11
-
Maybe IO lock method is what you are looking for: [lock](https://docs.raku.org/type/IO::Handle#method_lock) – LuVa Dec 28 '20 at 20:40
-
That's embarrassing. I looked all over and didn't see that. Thanks @ValleLukas! – JustThisGuy Dec 28 '20 at 21:06
2 Answers
1
I've come across these Raku-idiomatic phrases and use them a lot, with 'given' topicalizing for brevity/clarity:
Read:
given $path.IO.open {
.lock: :shared;
%data = from-json(.slurp);
.close;
}
Write:
given $path.IO.open(:w) {
.lock;
.spurt: to-json(%data);
.close;
}

markldevine
- 11
- 1
- 2
-
This doesn't unlock if the middle line throws an exception, a LEAVE phases may be helpful to ensure that – Leon Timmermans Jan 12 '21 at 01:55