The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

IPC::Notify

SYNOPSIS

  # Process 1- waits to be notified and then performs work
  my $notify = IPC::Notify->new("/path/to/lock");
  $notify->lock;
  for (;;) {
    $notify->wait;
    # do work
    print "I am doing some work!\n";
  }
  $notify->unlock;

  # Process 2- wakes up process 1
  my $notify = IPC::Notify->new("/path/to/lock");
  $notify->lock;
  $notify->notify;
  $notify->unlock;

CONSTRUCTOR

new() - create a new notify locking object

  my $notify = IPC::Notify->new($filename);

METHODS

is_locked() - check whether object is currently "locked"

  if ($notify->is_locked) { ... }

Returns nonzero if the object is currently locked.

lock() - obtain a file lock

  $notify->lock;

A lock must be acquired before using wait() or notify() on this object. This ensures proper synchronization. This method will block if another (non-waiting) process has the lock.

notify() - wake up all processes waiting on this lock

  $notify->notify;

This will wake up all processes waiting on the lock, however, you need to call unlock() from the notifying process before the other process(es) will be allowed to proceed.

wait() - wait for a notification on this lock

  $notify->wait($timeout_in_seconds);

This method will atomically give up the lock this process has on the object and wait for a notification. Before returning control, it will re-acquire the lock.

If $timeout_in_seconds is specified, wait() will return control early if a notification is not received within the specified time. Fractional values are acceptable.

If $timeout_in_seconds is absent, or "undef", then it will wait forever. If $timeout_in_seconds is zero, the call will be nonblocking. (It will simply indicate whether a notification has been received.)

The result is nonzero if a notification was received. Otherwise, the timeout had elapsed.

unlock() - release a lock

  $notify->unlock;

Be sure to unlock() if you are going to do some other work. As long as one process holds the lock, other processes will block to notify().