Skip to content

An utility library for process related functionality for PHP.

License

Notifications You must be signed in to change notification settings

rgalex2034/php-proc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

php-proc

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.

About

An utility library for process related functionality for PHP.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published