mmeyer2k / semlock
semaphores + closures
Installs: 252 280
Dependents: 1
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.0|^8.0
- ext-sysvsem: ^7.0|^8.0
This package is auto-updated.
Last update: 2024-10-25 05:18:22 UTC
README
A clean way to wrap semaphores around closures to perform exclusive execution of code. Semlock also allows for naming semaphones with descriptive strings instead of ints.
Read my blog posting about this tool here:
https://mmeyer2k.github.io/posts/php-exclusive-execution-closure-semaphore
Install
composer require mmeyer2k/semlock
Use
Basic Usage
\mmeyer2k\SemLock::synchronize('some_key', function () { $x = get_number_from_database(); $x++; sleep(5); save_number_to_database($x); });
Handling return values
The semlock
library will pass return values from the closure back to the calling context.
$returned = \mmeyer2k\SemLock::synchronize('some_key', function () { return 'something'; }); # $returned === 'something'