11

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?

JustThisGuy
  • 1,109
  • 5
  • 10

2 Answers2

12

IO::Handle has a lock and an unlock method to lock/unlock files. Locks can be exclusive or shared.

LuVa
  • 2,288
  • 2
  • 10
  • 20
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