Mojo::IOLoop::ReadWriteProcess::Shared::Lock(3pm) | User Contributed Perl Documentation | Mojo::IOLoop::ReadWriteProcess::Shared::Lock(3pm) |
Mojo::IOLoop::ReadWriteProcess::Shared::Lock - IPC Lock
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
Mojo::IOLoop::ReadWriteProcess::Shared::Lock uses IPC::Semaphore internally and creates a Lock from a semaphore that is available across different processes.
Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all events from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and implements the following new ones.
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 }
Acquire access to the lock and unlocks it.
"lock()" has the same arguments as Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore "acquire()".
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 }
Try to acquire lock in a non-blocking way.
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
Executes a function inside a locked section. Errors are caught so lock is released in case of failures.
Mojo::IOLoop::ReadWriteProcess::Shared::Lock inherits all attributes from Mojo::IOLoop::ReadWriteProcess::Shared::Semaphore and provides the following new ones.
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 flag for the lock. In such way you can limit the access to the lock, e.g. to specific user/group process.
use Mojo::IOLoop::ReadWriteProcess qw(lock); my $l = lock(key => 42);
Sets the lock key that is used to retrieve the lock among different processes, must be an integer.
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
You can set MOJO_PROCESS_DEBUG environment variable to get diagnostics about the process execution.
MOJO_PROCESS_DEBUG=1
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.
Ettore Di Giacinto <edigiacinto@suse.com>
2022-12-30 | perl v5.36.0 |