Mojo::...::Lock(3pm)
πΈ NAME πΈ
Mojo::IOLoop::ReadWriteProcess::Shared::Lock β it's an IPC Lock, bestie. That's the whole vibe. π
β¨ SYNOPSIS β¨
okay so picture this: you want a bunch of processes to share ONE lock, no chaos, no drama:
use Mojo::IOLoop::ReadWriteProcess qw(process queue lock);
my $q = queue; # Create a Queue
$q->pool->maximum_processes(10); # 10 Concurrent processes at maximum
$q->queue->maximum_processes(50); # 50 is maximum total to be allowed in the queue
$q->add(
process(
sub {
my $l = lock(key => 42); # IPC Lock
my $e = 1;
if ($l->lock) { # Blocking lock acquire
# Critical section
$e = 0;
$l->unlock;
}
exit($e);
}
)->set_pipes(0)->internal_pipes(0)) for 1 .. 20; # Fill with 20 processes
$q->consume(); # Consume the processes
twenty processes, one lock, zero fighting. we love to see it. π«Ά
π DESCRIPTION π
Mojo::IOLoop::ReadWriteProcess::Shared::Lock is lowkey doing magic under the hood β it uses IPC::Semaphore internally and creates a Lock from a semaphore that's available across different processes. so like, all your little process babies can share it. iconic behavior fr fr. β¨
π METHODS π
Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all events from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and implements the following new ones. periodt. π
π lock/unlock π
use Mojo::IOLoop::ReadWriteProcess qw(lock);
my $l = lock(key => "42"); # Create Lock with key 42
if ($l->lock) { # Blocking call
# Critical section
...
$l->unlock; # Release the lock
}
So what this does: it acquires access to the lock and unlocks it. (Yes, that's exactly what the page says, we're not editorializing, we're being β¨ faithful β¨.)
Also important: lock() has the same arguments as Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore acquire(). iykyk. π§
π try_lock π
use Mojo::IOLoop::ReadWriteProcess qw(lock);
my $l = lock(key => "42"); # Create Lock with key 42
if ($l->try_lock) { # Non Blocking call
# Critical section
...
$l->unlock; # Release the lock
}
It tries to acquire the lock in a non-blocking way. so if it can't get it, it just moves on with its life, no waiting around, main character energy only. π
π lock_section π
use Mojo::IOLoop::ReadWriteProcess qw(lock);
my $l = lock(key => 3331);
my $e = 1;
$l->lock_section(sub { $e = 0; die; }); # or also $l->section(sub { $e = 0 });
$l->locked; # is 0
This runs a function inside a locked section. and here's the best part β errors are caught, so the lock gets released even if things go sideways. no deadlocks, no crying. ππ«Ά
π ATTRIBUTES π
Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all attributes from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and provides the following new ones. slay. β¨
π© flags π©
use Mojo::IOLoop::ReadWriteProcess qw(lock);
use IPC::SysV qw(IPC_CREAT IPC_EXCL S_IRUSR S_IWUSR);
my $l = lock(flags=> IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR);
Sets the flag for the lock. and the way this works is β you can limit access to the lock, like, to a specific user or group process. gatekeeping, but make it permissions. π
π key π
use Mojo::IOLoop::ReadWriteProcess qw(lock);
my $l = lock(key => 42);
Sets the lock key, which is how different processes find and retrieve the same lock. must be an integer, no funny business. π’
π locked π
use Mojo::IOLoop::ReadWriteProcess qw(lock);
my $l = lock(key => 42);
$l->lock_section(sub {
$l->locked; # 1
});
$l->locked; # 0
Returns the lock status. that's it, that's the whole thing. β
π¦ DEBUGGING π¦
You can set the MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution. bestie detective mode. π
MOJO_PROCESS_DEBUG=1
π LICENSE π
Copyright (C) Ettore Di Giacinto.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. πΈ
π AUTHOR π
Ettore Di Giacinto <edigiacinto@suse.com>
and that's the whole page, babe π«Ά β this one comes from User Contributed Perl Documentation, perl v5.42.0, dated 2025-07-25. go forth and lock responsibly. π
β¨
π¦ where this page comes from
- fedora 43: perl-Mojo-IOLoop-ReadWriteProcess 1.1.0-2.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms