nyugodt/proc

An utility library for process related functionality for PHP.

v1.1.0 2020-11-04 22:05 UTC

This package is auto-updated.

Last update: 2025-04-12 21:00:32 UTC


README

An utility library for process related functionality for PHP.

Dependencies

This library depends on the semaphore, shared memory and ipc functions of PHP for the use of the SemLock class, which are not available on Windows.

Usage

This library provides two classes, FileLock and SemLock. Both works in the same way. They can be instantiated by a ::newInstance() method, which accepts an optional parameter as a path. If a lock is created with the same path in different processes, only one of them will be able to execute code inside the synchronize() call. Once a process ends it's synchronize() call, it releases the lock and another process takes the execution.

If no path is provided, it gets the full path of the calling file.

<?php
$lock = \Nyugodt\Proc\FileLock::newInstance();
$lock->synchronize(function(){
  echo "This function will not be executed by two threads at the same time.\n";
  sleep(3);
  echo "Process ended!\n";
});

If this example code gets executed by multiple processes, every process will wait the current running process who grabbed the lock first until the sleep(3) call ends and exits the synchronize() call.